upload_json.ashx.exclude 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <%@ webhandler Language="C#" class="Upload" %>
  2. /**
  3. * KindEditor ASP.NET
  4. *
  5. * 本ASP.NET程序是演示程序,建议不要直接在实际项目中使用。
  6. * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。
  7. *
  8. */
  9. using System;
  10. using System.Collections;
  11. using System.Web;
  12. using System.IO;
  13. using System.Globalization;
  14. using LitJson;
  15. public class Upload : IHttpHandler
  16. {
  17. private HttpContext context;
  18. public void ProcessRequest(HttpContext context)
  19. {
  20. String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("/") + 1);
  21. //文件保存目录路径
  22. String savePath = "../../../UploadAttach/";
  23. //文件保存目录URL
  24. String saveUrl = aspxUrl + "../../../UploadAttach/";
  25. //定义允许上传的文件扩展名
  26. Hashtable extTable = new Hashtable();
  27. extTable.Add("image", "gif,jpg,jpeg,png,bmp");
  28. extTable.Add("flash", "swf,flv");
  29. extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
  30. extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
  31. //最大文件大小
  32. int maxSize = 1000000;
  33. this.context = context;
  34. HttpPostedFile imgFile = context.Request.Files["imgFile"];
  35. if (imgFile == null)
  36. {
  37. showError("请选择文件。");
  38. }
  39. String dirPath = context.Server.MapPath(savePath);
  40. if (!Directory.Exists(dirPath))
  41. {
  42. showError("上传目录不存在。");
  43. }
  44. String dirName = context.Request.QueryString["dir"];
  45. if (String.IsNullOrEmpty(dirName)) {
  46. dirName = "image";
  47. }
  48. if (!extTable.ContainsKey(dirName)) {
  49. showError("目录名不正确。");
  50. }
  51. String fileName = imgFile.FileName;
  52. String fileExt = Path.GetExtension(fileName).ToLower();
  53. if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
  54. {
  55. showError("上传文件大小超过限制。");
  56. }
  57. if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
  58. {
  59. showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
  60. }
  61. //创建文件夹
  62. dirPath += dirName + "/";
  63. saveUrl += dirName + "/";
  64. if (!Directory.Exists(dirPath)) {
  65. Directory.CreateDirectory(dirPath);
  66. }
  67. String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
  68. dirPath += ymd + "/";
  69. saveUrl += ymd + "/";
  70. if (!Directory.Exists(dirPath)) {
  71. Directory.CreateDirectory(dirPath);
  72. }
  73. String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
  74. String filePath = dirPath + newFileName;
  75. imgFile.SaveAs(filePath);
  76. String fileUrl = saveUrl + newFileName;
  77. Hashtable hash = new Hashtable();
  78. hash["error"] = 0;
  79. hash["url"] = fileUrl;
  80. context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
  81. context.Response.Write(JsonMapper.ToJson(hash));
  82. context.Response.End();
  83. }
  84. private void showError(string message)
  85. {
  86. Hashtable hash = new Hashtable();
  87. hash["error"] = 1;
  88. hash["message"] = message;
  89. context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
  90. context.Response.Write(JsonMapper.ToJson(hash));
  91. context.Response.End();
  92. }
  93. public bool IsReusable
  94. {
  95. get
  96. {
  97. return true;
  98. }
  99. }
  100. }