Ueditor在MVC中上传文件和远程图片下载的脚本


  1 [HttpPost]
  2 public ActionResult UpImage(long id = 0) {
  3     //上传配置
  4     const int size = 2; // 文件大小限制,单位MB
  5     string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" }; //文件允许格式
  6     var savePath = string.Format("/upload/taobao/{0}/", id); //保存文件地址
  7     IoHelper.Folder(savePath);
  8 
  9     var tmp = "{{'url':'{0}','title':'" + Request.Form["pictitle"] + "','original':'" + Request.Form["fileName"] + "','state':'{1}'}}";
 10 
 11     try {
 12         var file = Request.Files[0];
 13         if (file == null || file.ContentLength < 100)
 14             return Content(string.Format(tmp, "", "请选择上传文件!"));
 15 
 16         var temp = file.FileName;
 17         var ext = IoHelper.Ext(temp);
 18 
 19         if (!filetype.Contains(ext)) // 格式验证
 20             return Content(string.Format(tmp, "", "不允许的文件类型!"));
 21 
 22         if (file.ContentLength > size * 1024 * 1024) // 大小验证
 23             return Content(string.Format(tmp, "", "文件大小超出网站限制!"));
 24 
 25         savePath += temp.Md5() + ext;
 26         file.SaveAs(IoHelper.MapPath(savePath));
 27         return Content(string.Format(tmp, savePath, "上传成功!"));
 28     } catch (Exception e) {
 29         LogHelper.GetInstance().WriteToLog(e);
 30         return Content(string.Format(tmp, "", "上传失败!"));
 31     }
 32 }
 33 
 34 [HttpPost]
 35 public ActionResult GetImage(long id = 0) {
 36     var savePath = string.Format("/upload/taobao/{0}/", id); //保存文件地址
 37     IoHelper.Folder(savePath); // 创建保存位置
 38 
 39     string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };             //文件允许格式
 40     const int fileSize = 3000; //文件大小限制,单位kb
 41 
 42     var uri = Server.HtmlEncode(Request["upfile"]);
 43     uri = uri.Replace("&amp;", "&");
 44     var imgUrls = Regex.Split(uri, "ue_separate_ue", RegexOptions.IgnoreCase);
 45 
 46     var tmpNames = new ArrayList();
 47     var wc = new WebClient();
 48 
 49     try {
 50         for (int i = 0, len = imgUrls.Length; i < len; i++) {
 51             var imgUrl = imgUrls[i];
 52 
 53             if (imgUrl.Substring(0, 7) != "http://") {
 54                 tmpNames.Add("url error");
 55                 continue;
 56             }
 57 
 58             var currentType = IoHelper.Ext(imgUrl);
 59             if (!filetype.Contains(currentType)) { // 格式验证
 60                 tmpNames.Add("ext error");
 61                 continue;
 62             }
 63 
 64             var res = (HttpWebResponse)WebRequest.Create(imgUrl).GetResponse();
 65             if (res.ResponseUri.Scheme.ToLower().Trim() != "http") { // http 检测
 66                 tmpNames.Add("http error");
 67                 continue;
 68             }
 69 
 70             if (res.ContentLength > fileSize * 1024) { //大小验证
 71                 tmpNames.Add("size error");
 72                 continue;
 73             }
 74 
 75             if (res.StatusCode != HttpStatusCode.OK) { // 死链验证
 76                 tmpNames.Add("status error");
 77                 continue;
 78             }
 79             //检查mime类型
 80             if (!res.ContentType.Contains("image")) {
 81                 tmpNames.Add("type error");
 82                 continue;
 83             }
 84             res.Close();
 85 
 86             var imgPath = savePath + imgUrl.Md5() + currentType;
 87             tmpNames.Add(imgPath);
 88 
 89             if (IoHelper.Exists(imgPath))
 90                 continue;
 91 
 92             //写入文件
 93             wc.DownloadFile(imgUrl, IoHelper.MapPath(imgPath));
 94         }
 95     } catch (Exception ex) {
 96         LogHelper.GetInstance().WriteToLog(ex);
 97         tmpNames.Add("error ex");
 98     } finally {
 99         wc.Dispose();
100     }
101     return Content("{url:'" + ConverToString(tmpNames) + "',tip:'远程图片抓取成功!',srcUrl:'" + uri + "'}");
102 }
103 
104 //集合转换字符串
105 private static string ConverToString(ArrayList tmpNames) {
106     var str = String.Empty;
107     for (int i = 0, len = tmpNames.Count; i < len; i++) {
108         str += tmpNames[i] + "ue_separate_ue";
109         if (i == tmpNames.Count - 1)
110             str += tmpNames[i];
111     }
112     return str;
113 }

优质内容筛选与推荐>>
1、数据结构和算法基础之队列的链式存储
2、jQuery,javascript获得网页的高度和宽度
3、CZGL.Auth: ASP.NET Core Jwt角色授权快速配置库
4、xamarin.android 给View控件 添加数字提醒效果-BadgeView
5、MSP430F5529学习记录


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号