UploadHandler.cs 5.6 KB

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