zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Windows进程通信(2)使用内存映射文件
    VC2010添加头文件目录
    CString(ANSI/Unicode)与string/wstring的安全转换
    1005 ( Number Sequence )
    1004 ( Let the Balloon Rise )
    1003 ( Max Sum )
    CreateMutex
    CloseHandle
    delphi的Frame简单演示
    DLL中显示模式窗体
  • 原文地址:https://www.cnblogs.com/flys5200/p/3100329.html
Copyright © 2011-2022 走看看