示例#1
0
        public ActionResult FileUpload(string qqfile, string dir)
        {
            var    physicalPath = Server.MapPath(dir);
            string returnFile;

            try
            {
                var    stream = Request.InputStream;
                string file;
                if (String.IsNullOrEmpty(Request["qqfile"]))
                {
                    // IE
                    var postedFile = Request.Files[0];
                    stream     = postedFile.InputStream;
                    file       = Path.Combine(physicalPath, Path.GetFileName(postedFile.FileName));
                    returnFile = postedFile.FileName;
                }
                else
                {
                    //Webkit, Mozilla
                    file       = Path.Combine(physicalPath, qqfile);
                    returnFile = qqfile;
                }

                using (var img = System.Drawing.Image.FromStream(stream))
                {
                    var imageUploadSetting = (ImageUploadSetting)_settingServices.LoadSetting <ImageUploadSettingResolver>();
                    if (imageUploadSetting != null)
                    {
                        if (imageUploadSetting.MinWidth.HasValue && img.Width < imageUploadSetting.MinWidth)
                        {
                            return(Json(new { Success = false, Message = string.Format(_localizedResourceServices.T("AdminModule:::Media:::Upload:::ValidationMessages:::InvalidMinWidth:::Image Width is less than {0}"), imageUploadSetting.MinWidth) }, "text/html"));
                        }
                        if (imageUploadSetting.MinHeight.HasValue && img.Height < imageUploadSetting.MinHeight)
                        {
                            return(Json(new { Success = false, Message = string.Format(_localizedResourceServices.T("AdminModule:::Media:::Upload:::ValidationMessages:::InvalidMinHeight:::Image Height is less than {0}"), imageUploadSetting.MinHeight) }, "text/html"));
                        }
                        if (imageUploadSetting.MaxWidth.HasValue && img.Width > imageUploadSetting.MaxWidth)
                        {
                            return(Json(new { Success = false, Message = string.Format(_localizedResourceServices.T("AdminModule:::Media:::Upload:::ValidationMessages:::InvalidMaxWidth:::Image Width is greater than {0}"), imageUploadSetting.MaxWidth) }, "text/html"));
                        }
                        if (imageUploadSetting.MaxHeight.HasValue && img.Height > imageUploadSetting.MaxHeight)
                        {
                            return(Json(new { Success = false, Message = string.Format(_localizedResourceServices.T("AdminModule:::Media:::Upload:::ValidationMessages:::InvalidMaxHeight:::Image Height is greater than {0}"), imageUploadSetting.MaxHeight) }, "text/html"));
                        }
                    }
                    img.Save(file);
                }
            }
            catch (Exception ex)
            {
                while (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                }
                return(Json(new { Success = false, Message = ex.Message }, "text/html"));
            }

            var isImage  = false;
            var position = returnFile.LastIndexOf(".", StringComparison.Ordinal);

            if (position > 0)
            {
                if (_mediaServices.Images.Contains(returnFile.Substring(position)))
                {
                    isImage = true;
                }
            }

            var location = string.Format("{0}/{1}", dir, returnFile);

            return(Json(new { Success = true, Message = _localizedResourceServices.T("AdminModule:::Media:::Upload:::Messages:::Upload successfully."), fileLocation = location, isImage }, "text/html"));
        }