Uploader.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.IO;
  5. using System.Collections;
  6. /// <summary>
  7. /// UEditor编辑器通用上传类
  8. /// </summary>
  9. public class Uploader
  10. {
  11. string state = "SUCCESS";
  12. string URL = null;
  13. string currentType = null;
  14. string uploadpath = null;
  15. string filename = null;
  16. string originalName = null;
  17. HttpPostedFile uploadFile = null;
  18. /**
  19. * 上传文件的主处理方法
  20. * @param HttpContext
  21. * @param string
  22. * @param string[]
  23. *@param int
  24. * @return Hashtable
  25. */
  26. public Hashtable upFile(HttpContext cxt, string pathbase, string[] filetype, int size)
  27. {
  28. pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
  29. uploadpath = cxt.Server.MapPath(pathbase);//获取文件上传路径
  30. try
  31. {
  32. uploadFile = cxt.Request.Files[0];
  33. originalName = uploadFile.FileName;
  34. //目录创建
  35. createFolder();
  36. //格式验证
  37. if (checkType(filetype))
  38. {
  39. state = "不允许的文件类型";
  40. }
  41. //大小验证
  42. if (checkSize(size))
  43. {
  44. state = "文件大小超出网站限制";
  45. }
  46. //保存图片
  47. if (state == "SUCCESS")
  48. {
  49. filename = reName();
  50. uploadFile.SaveAs(uploadpath + filename);
  51. URL = pathbase + filename;
  52. }
  53. }
  54. catch (Exception e)
  55. {
  56. state = "未知错误";
  57. URL = "";
  58. }
  59. return getUploadInfo();
  60. }
  61. /**
  62. * 上传涂鸦的主处理方法
  63. * @param HttpContext
  64. * @param string
  65. * @param string[]
  66. *@param string
  67. * @return Hashtable
  68. */
  69. public Hashtable upScrawl(HttpContext cxt, string pathbase, string tmppath, string base64Data)
  70. {
  71. pathbase = pathbase + DateTime.Now.ToString("yyyy-MM-dd") + "/";
  72. uploadpath = cxt.Server.MapPath(pathbase);//获取文件上传路径
  73. FileStream fs = null;
  74. try
  75. {
  76. //创建目录
  77. createFolder();
  78. //生成图片
  79. filename = System.Guid.NewGuid() + ".png";
  80. fs = File.Create(uploadpath + filename);
  81. byte[] bytes = Convert.FromBase64String(base64Data);
  82. fs.Write(bytes, 0, bytes.Length);
  83. URL = pathbase + filename;
  84. }
  85. catch (Exception e)
  86. {
  87. state = "未知错误";
  88. URL = "";
  89. }
  90. finally
  91. {
  92. fs.Close();
  93. deleteFolder(cxt.Server.MapPath(tmppath));
  94. }
  95. return getUploadInfo();
  96. }
  97. /**
  98. * 获取文件信息
  99. * @param context
  100. * @param string
  101. * @return string
  102. */
  103. public string getOtherInfo(HttpContext cxt, string field)
  104. {
  105. string info = null;
  106. if (cxt.Request.Form[field] != null && !String.IsNullOrEmpty(cxt.Request.Form[field]))
  107. {
  108. info = field == "fileName" ? cxt.Request.Form[field].Split(',')[1] : cxt.Request.Form[field];
  109. }
  110. return info;
  111. }
  112. /**
  113. * 获取上传信息
  114. * @return Hashtable
  115. */
  116. private Hashtable getUploadInfo()
  117. {
  118. Hashtable infoList = new Hashtable();
  119. infoList.Add("state", state);
  120. infoList.Add("url", URL);
  121. infoList.Add("originalName", originalName);
  122. infoList.Add("name", Path.GetFileName(URL));
  123. infoList.Add("size", uploadFile.ContentLength);
  124. infoList.Add("type", Path.GetExtension(originalName));
  125. return infoList;
  126. }
  127. /**
  128. * 重命名文件
  129. * @return string
  130. */
  131. private string reName()
  132. {
  133. return System.Guid.NewGuid() + getFileExt();
  134. }
  135. /**
  136. * 文件类型检测
  137. * @return bool
  138. */
  139. private bool checkType(string[] filetype)
  140. {
  141. currentType = getFileExt();
  142. return Array.IndexOf(filetype, currentType) == -1;
  143. }
  144. /**
  145. * 文件大小检测
  146. * @param int
  147. * @return bool
  148. */
  149. private bool checkSize(int size)
  150. {
  151. return uploadFile.ContentLength >= (size * 1024 * 1024);
  152. }
  153. /**
  154. * 获取文件扩展名
  155. * @return string
  156. */
  157. private string getFileExt()
  158. {
  159. string[] temp = uploadFile.FileName.Split('.');
  160. return "." + temp[temp.Length - 1].ToLower();
  161. }
  162. /**
  163. * 按照日期自动创建存储文件夹
  164. */
  165. private void createFolder()
  166. {
  167. if (!Directory.Exists(uploadpath))
  168. {
  169. Directory.CreateDirectory(uploadpath);
  170. }
  171. }
  172. /**
  173. * 删除存储文件夹
  174. * @param string
  175. */
  176. public void deleteFolder(string path)
  177. {
  178. //if (Directory.Exists(path))
  179. //{
  180. // Directory.Delete(path, true);
  181. //}
  182. }
  183. }