首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。