| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using System;
- using System.Collections;
- using System.Globalization;
- using System.IO;
- using System.Web;
- namespace CommonTool
- {
- public class UploadFileHelper
- {
- public static UploadFileHelper Instance { get; } = new UploadFileHelper();
- public UploadResult UploadFiles(string filePath, string fileType, HttpRequestBase request,HttpServerUtilityBase server)
- {
- UploadResult result = new UploadResult
- {
- FileUrls = new ArrayList(),
- Success = false,
- Msg = "Failure!",
- VirtualPathUrls = new ArrayList(),
- };
- //遍历所有文件域
- foreach (string fieldName in request.Files.AllKeys)
- {
- HttpPostedFileBase file = request.Files.Get(fieldName);
- string ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
- filePath += ymd + "/";
- string fileName = DateTime.Now.ToString("yyyyMMddhhmmss_ffff");
- string urlPath = $"{filePath}{fileName}.{fileType}";
- string virtualPath= server.MapPath(urlPath);
- try
- {
- if (!Directory.Exists(server.MapPath(filePath)))
- Directory.CreateDirectory(server.MapPath(filePath));
- file?.SaveAs(virtualPath);
- result.FileUrls.Add(urlPath);
- result.VirtualPathUrls.Add(virtualPath);
- result.Success = true;
- result.Msg = "Success!";
- }
- catch (Exception e)
- {
- result.Msg = e.Message;
- return result;
- }
- }
- return result;
- }
- public UploadResult UploadFiles2(string filePath, HttpPostedFileBase requestFile, HttpServerUtilityBase server)
- {
- UploadResult result = new UploadResult
- {
- FileUrls = new ArrayList(),
- Success = false,
- Msg = "Failure!"
- };
- //遍历所有文件域
- string urlPath = $"{filePath}";
- string virtualPath = server.MapPath(urlPath);
- try
- {
- if (File.Exists(virtualPath))
- File.Delete(virtualPath);
- requestFile?.SaveAs(virtualPath);
- result.FileUrls.Add(urlPath);
- result.VirtualPathUrls.Add(virtualPath);
- result.Success = true;
- result.Msg = "Success!";
- }
- catch (Exception e)
- {
- result.Msg = e.Message;
- return result;
- }
- return result;
- }
- }
- public class UploadResult
- {
- /// <summary>
- /// 表示文件是否已上传成功。
- /// </summary>
- // ReSharper disable once InconsistentNaming
- public bool Success;
- /// <summary>
- /// 自定义的附加消息。
- /// </summary>
- // ReSharper disable once InconsistentNaming
- public string Msg;
-
- /// <summary>
- /// 表示所有文件的保存地址,该变量为一个数组。
- /// </summary>
- // ReSharper disable once InconsistentNaming
- public ArrayList FileUrls;
- public ArrayList VirtualPathUrls;
- }
- public class UploadKindEditor
- {
- public static UploadKindEditor Instance { get; } = new UploadKindEditor();
- public HttpContextBase Context { get; set; }
- public string KindEditorUpload(string filePath, HttpContextBase context)
- {
- Context = context;
- string dirPath = filePath;//文件保存目录路径
- int maxSize = 10*1024*1024;//最大文件大小
- string dirName = context.Request.QueryString["dir"];
- string fileUrl;
- //定义允许上传的文件扩展名
- Hashtable extTable = new Hashtable
- {
- {"image", "gif,jpg,jpeg,png,bmp"},
- {"flash", "swf,flv"},
- {"media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"},
- {"file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"}
- };
- HttpPostedFileBase imgFile = context.Request.Files["imgFile"];
-
- //if (!Directory.Exists(dirPath))
- //{
- // showError("上传目录不存在。");
- //}
- if (string.IsNullOrEmpty(dirName))
- {
- dirName = "image";
- }
- if (!extTable.ContainsKey(dirName))
- {
- return showError("目录名不正确。");
- }
- if (imgFile == null)
- {
- return showError("请选择文件。");
- }
- string fileName = imgFile.FileName;
- string fileExt = Path.GetExtension(fileName)?.ToLower();
- if (imgFile.InputStream.Length > maxSize)
- {
- return showError("上传文件大小超过限制。");
- }
- if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(((string)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
- {
- return showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((string)extTable[dirName]) + "格式。");
- }
-
- //创建文件夹
- dirPath += dirName + "/";
- var savePath = context.Server.MapPath(dirPath);
- if (!Directory.Exists(savePath))
- {
- Directory.CreateDirectory(savePath);
- }
- string ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
- dirPath += ymd + "/";
- savePath = context.Server.MapPath(dirPath);
- if (!Directory.Exists(savePath))
- {
- Directory.CreateDirectory(savePath);
- }
- string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
- filePath = savePath + newFileName;
- imgFile.SaveAs(filePath);
- fileUrl = dirPath + newFileName;
- Hashtable hash = new Hashtable
- {
- ["error"] = 0,
- ["url"] = fileUrl
- };
- return hash.ToJson();
- }
- private string showError(string message)
- {
- Hashtable hash = new Hashtable
- {
- ["error"] = 1,
- ["message"] = message
- };
- return hash.ToJson();
- }
-
- }
- }
|