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         }

 

转载于:https://www.cnblogs.com/baiyunchen/p/4010554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值