You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.3 KiB

2 years ago
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Web;
  5. namespace EasyBL.Handler
  6. {
  7. /// <summary>
  8. /// UploadHandler 的摘要说明
  9. /// </summary>
  10. public class UploadHandler : Handler
  11. {
  12. public UploadConfig UploadConfig { get; private set; }
  13. public UploadResult Result { get; private set; }
  14. public UploadHandler(HttpContext context, UploadConfig config)
  15. : base(context)
  16. {
  17. this.UploadConfig = config;
  18. this.Result = new UploadResult() { State = UploadState.Unknown };
  19. }
  20. public override void Process()
  21. {
  22. byte[] uploadFileBytes = null;
  23. string uploadFileName = null;
  24. if (UploadConfig.Base64)
  25. {
  26. uploadFileName = UploadConfig.Base64Filename;
  27. uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
  28. }
  29. else
  30. {
  31. var file = Request.Files[UploadConfig.UploadFieldName];
  32. uploadFileName = file.FileName;
  33. if (!CheckFileType(uploadFileName))
  34. {
  35. Result.State = UploadState.TypeNotAllow;
  36. WriteResult();
  37. return;
  38. }
  39. if (!CheckFileSize(file.ContentLength))
  40. {
  41. Result.State = UploadState.SizeLimitExceed;
  42. WriteResult();
  43. return;
  44. }
  45. uploadFileBytes = new byte[file.ContentLength];
  46. try
  47. {
  48. file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
  49. }
  50. catch (Exception)
  51. {
  52. Result.State = UploadState.NetworkError;
  53. WriteResult();
  54. }
  55. }
  56. Result.OriginFileName = uploadFileName;
  57. var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
  58. var localPath = Server.MapPath(savePath);
  59. try
  60. {
  61. if (!Directory.Exists(Path.GetDirectoryName(localPath)))
  62. {
  63. Directory.CreateDirectory(Path.GetDirectoryName(localPath));
  64. }
  65. File.WriteAllBytes(localPath, uploadFileBytes);
  66. Result.Url = savePath;
  67. Result.State = UploadState.Success;
  68. }
  69. catch (Exception e)
  70. {
  71. Result.State = UploadState.FileAccessError;
  72. Result.ErrorMessage = e.Message;
  73. }
  74. finally
  75. {
  76. WriteResult();
  77. }
  78. }
  79. private void WriteResult()
  80. {
  81. this.WriteJson(new
  82. {
  83. state = GetStateMessage(Result.State),
  84. url = Result.Url,
  85. title = Result.OriginFileName,
  86. original = Result.OriginFileName,
  87. error = Result.ErrorMessage
  88. });
  89. }
  90. private static string GetStateMessage(UploadState state)
  91. {
  92. switch (state)
  93. {
  94. case UploadState.Success:
  95. return "SUCCESS";
  96. case UploadState.FileAccessError:
  97. return "文件访问出错,请检查写入权限";
  98. case UploadState.SizeLimitExceed:
  99. return "文件大小超出服务器限制";
  100. case UploadState.TypeNotAllow:
  101. return "不允许的文件格式";
  102. case UploadState.NetworkError:
  103. return "网络错误";
  104. default:
  105. return "未知错误";
  106. }
  107. }
  108. private bool CheckFileType(string filename)
  109. {
  110. var fileExtension = Path.GetExtension(filename).ToLower();
  111. return UploadConfig.AllowExtensions.Select(x => x.ToLower()).Contains(fileExtension);
  112. }
  113. private bool CheckFileSize(int size)
  114. {
  115. return size < UploadConfig.SizeLimit;
  116. }
  117. }
  118. public class UploadConfig
  119. {
  120. /// <summary>
  121. /// 文件命名规则
  122. /// </summary>
  123. public string PathFormat { get; set; }
  124. /// <summary>
  125. /// 上传表单域名称
  126. /// </summary>
  127. public string UploadFieldName { get; set; }
  128. /// <summary>
  129. /// 上传大小限制
  130. /// </summary>
  131. public int SizeLimit { get; set; }
  132. /// <summary>
  133. /// 上传允许的文件格式
  134. /// </summary>
  135. public string[] AllowExtensions { get; set; }
  136. /// <summary>
  137. /// 文件是否以 Base64 的形式上传
  138. /// </summary>
  139. public bool Base64 { get; set; }
  140. /// <summary>
  141. /// Base64 字符串所表示的文件名
  142. /// </summary>
  143. public string Base64Filename { get; set; }
  144. }
  145. public class UploadResult
  146. {
  147. public UploadState State { get; set; }
  148. public string Url { get; set; }
  149. public string OriginFileName { get; set; }
  150. public string ErrorMessage { get; set; }
  151. }
  152. public enum UploadState
  153. {
  154. Success = 0,
  155. SizeLimitExceed = -1,
  156. TypeNotAllow = -2,
  157. FileAccessError = -3,
  158. NetworkError = -4,
  159. Unknown = 1,
  160. }
  161. }