.NET MVC 图片上传

来源:互联网 发布:模仿声音软件下载 编辑:程序博客网 时间:2024/05/16 05:46
public class ImageHelperController :Controller    {        //        // GET: /Admin/ImageHelper/        public JsonResult UploadImage(HttpPostedFileBase imgFile)        {            object result = ETAGTech.Fmk.PMN.SubDispatch.BSIsTimeOut();            if (result == null)            {                try                {                    if (imgFile != null)                    { //新上传了图片,需要保存处理                        string fImageName = string.Empty;                        object fResult = ImageHelperController.SaveImage(imgFile, ref fImageName, false);                        if (fResult == null)                        {//上传成功                            return Json(new { error = 0, url = "/Upload/Images/" + fImageName }, "text/html");                        }                        else                        {//上传失败                            return Json(new { error = 1, message = "上传失败!" }, "text/html");                        }                    }                }                catch (Exception ex)                {                    result = new { error = 1, message = ex.ToString() };                }            }            else            {                result = new { error = 1, message = "没有文件!" };            }            return Json(result, "text/html");        }        /// <summary>        /// .NET MVC模式下的图片上传        /// </summary>        /// <param name="aImage">要上传的图片</param>        /// <param name="ImageName">上传成功后的路径</param>        /// <param name="SaveThumb">是否保存</param>        /// <returns>返回null表示上传成功</returns>        public static object SaveImage(HttpPostedFileBase aImage, ref string ImageName, bool SaveThumb)        {            object result = new object();            string ExtName = Path.GetExtension(aImage.FileName).ToLower();            if (ExtName == ".jpg" || ExtName == ".jpeg" || ExtName == ".png" || ExtName == ".gif")            {                int fMaxImageSize = Convert.ToInt32(ConfigurationManager.AppSettings["MaxImageSize"]);//单位 kb                if (aImage.ContentLength > fMaxImageSize * 1024)                {                    result = new { Result = false, Msg = "图片超过限定大小!" };                }                else                {                    //ImageName = Guid.NewGuid().ToString() + ExtName;//图片名称                    ImageName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ExtName;//图片名称                    string UploadFolder = AppDomain.CurrentDomain.BaseDirectory + "Upload/Images/";                    if (!Directory.Exists(UploadFolder))                    {                        Directory.CreateDirectory(UploadFolder);//若不存图片文件夹则创建                    }                    Image fSaveImage = Image.FromStream(aImage.InputStream);                    fSaveImage.Save(UploadFolder + ImageName);                    if (SaveThumb == true)                    {                        string ThumbUploadFolder = AppDomain.CurrentDomain.BaseDirectory + "Upload/Images/Thumb/";                        int ThumbImageLength = int.Parse(ConfigurationManager.AppSettings["ThumbImageLength"]);                        if (fSaveImage.Height > ThumbImageLength || fSaveImage.Width > ThumbImageLength)                        {//原图高宽至少一项大于设定值                            Image fThumb;                            if (fSaveImage.Height > fSaveImage.Width)                            {//高大于宽,以高为主                                double p = (double)fSaveImage.Height / (double)ThumbImageLength;                                fThumb = fSaveImage.GetThumbnailImage((int)(fSaveImage.Width / p),                                    ThumbImageLength,                                    new Image.GetThumbnailImageAbort(ThumbnailCallback),                                    IntPtr.Zero);                            }                            else                            { //宽大于高,以宽为主                                double p = (double)fSaveImage.Width / (double)ThumbImageLength;                                fThumb = fSaveImage.GetThumbnailImage(ThumbImageLength,                                    (int)(fSaveImage.Height / p),                                    new Image.GetThumbnailImageAbort(ThumbnailCallback),                                    IntPtr.Zero);                            }                            fThumb.Save(ThumbUploadFolder + ImageName);                        }                        else                        {                            fSaveImage.Save(ThumbUploadFolder + ImageName);                        }                    }                    fSaveImage.Dispose();                    return null;                }            }            else            {                result = new { Result = false, Msg = "图片格式不正确,只允许上传jpg,png,gif!" };            }            return result;        }        public static bool ThumbnailCallback()        {            return false;        }        /// <summary>        /// 删除图片        /// </summary>        /// <param name="aImageName">要删除的图片名称</param>        public static void DeleteImage(string aImageName)        {            //删除旧图片            if (!string.IsNullOrEmpty(aImageName))            { //原图片不是默认图片,需要清理                string fImagePath = AppDomain.CurrentDomain.BaseDirectory + "Upload/Images/" + aImageName;                string fImageThumbPath = AppDomain.CurrentDomain.BaseDirectory + "Upload/Images/Thumb/" + aImageName;                if (System.IO.File.Exists(fImagePath))                {                    System.IO.File.Delete(fImagePath);                }                if (System.IO.File.Exists(fImageThumbPath))                {                    System.IO.File.Delete(fImageThumbPath);                }            }        }

web.config

  <appSettings>    <!-- 最大上传图片大小 单位:KB-->    <add key="MaxImageSize" value="1024" />    <!-- 缩略图边长-->    <add key="ThumbImageLength" value="120" />  </appSettings>

0 0
原创粉丝点击