首页 > 代码库 > Kindeditor图片上传Controller

Kindeditor图片上传Controller

asp.net MVC Kindeditor 图片、文件上传所用的Controller

 1  [HttpPost, AllowAnonymous] 2         public ActionResult UploadImage() 3         { 4             string savePath = VirtualPaths.ImgDir + "/Upload/"; 5             string saveUrl = "/Upload/"; 6             string fileTypes = "gif,jpg,jpeg,png,bmp"; 7             int maxSize = 1000000; 8  9             var hash = new Hashtable();10 11             HttpPostedFileBase file = Request.Files["imgFile"];12             if (file == null)13             {14                 hash = new Hashtable();15                 hash["error"] = 1;16                 hash["message"] = "请选择文件";17                 return Json(hash, "text/html;charset=UTF-8");18             }19 20             string dirPath = savePath;21             if (!Directory.Exists(dirPath))22             {23                 Directory.CreateDirectory(dirPath);24             }25 26             string fileName = file.FileName;27             string fileExt = Path.GetExtension(fileName).ToLower();28 29             ArrayList fileTypeList = ArrayList.Adapter(fileTypes.Split(,));30 31             if (file.InputStream == null || file.InputStream.Length > maxSize)32             {33                 hash = new Hashtable();34                 hash["error"] = 1;35                 hash["message"] = "上传文件大小超过限制";36                 return Json(hash, "text/html;charset=UTF-8");37             }38 39             if (string.IsNullOrEmpty(fileExt) ||40                 Array.IndexOf(fileTypes.Split(,), fileExt.Substring(1).ToLower()) == -1)41             {42                 hash = new Hashtable();43                 hash["error"] = 1;44                 hash["message"] = "上传文件扩展名是不允许的扩展名";45                 return Json(hash, "text/html;charset=UTF-8");46             }47 48             string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) +49                                  fileExt;50             string filePath = dirPath + newFileName;51             file.SaveAs(filePath);52             string fileUrl = saveUrl + newFileName;53 54             hash = new Hashtable();55             hash["error"] = 0;56             hash["url"] = fileUrl;57 58             return Json(hash, "text/html;charset=UTF-8");59         }

 

Kindeditor图片上传Controller