upload_json1.ashx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 = "/up/";
  23. //文件保存目录URL
  24. String saveUrl = "/up/";
  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,mp4");
  30. extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
  31. //最大文件大小
  32. long maxSize = 1073741824000;
  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. Directory.CreateDirectory(dirPath);
  44. }
  45. String dirName = context.Request.QueryString["dir"];
  46. if (String.IsNullOrEmpty(dirName))
  47. {
  48. dirName = "image";
  49. }
  50. if (!extTable.ContainsKey(dirName))
  51. {
  52. showError("目录名不正确。");
  53. }
  54. String fileName = imgFile.FileName;
  55. String fileExt = Path.GetExtension(fileName).ToLower();
  56. if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
  57. {
  58. showError("上传文件大小超过限制。");
  59. }
  60. if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
  61. {
  62. showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
  63. }
  64. //创建文件夹
  65. dirPath += dirName + "/";
  66. saveUrl += dirName + "/";
  67. if (!Directory.Exists(dirPath))
  68. {
  69. Directory.CreateDirectory(dirPath);
  70. }
  71. String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
  72. dirPath += ymd + "/";
  73. saveUrl += ymd + "/";
  74. if (!Directory.Exists(dirPath))
  75. {
  76. Directory.CreateDirectory(dirPath);
  77. }
  78. String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
  79. String filePath = dirPath + newFileName;
  80. imgFile.SaveAs(filePath);
  81. String fileUrl = saveUrl + newFileName;
  82. if (dirName != "media")
  83. {
  84. if (!Library.function.check_pic(filePath))
  85. {
  86. File.Delete(filePath);
  87. context.Response.End();
  88. }
  89. if (Library.function.getImgRule(filePath)["width"] > 1000)
  90. {
  91. String newFileName_sl = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + "_sl" + fileExt;
  92. Library.function.imgcut(1000, filePath, dirPath + newFileName_sl);
  93. File.Delete(filePath);
  94. fileUrl = saveUrl + newFileName_sl;
  95. }
  96. }
  97. Hashtable hash = new Hashtable();
  98. hash["error"] = 0;
  99. hash["url"] = fileUrl;
  100. context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
  101. context.Response.Write(JsonMapper.ToJson(hash));
  102. context.Response.End();
  103. }
  104. private void showError(string message)
  105. {
  106. Hashtable hash = new Hashtable();
  107. hash["error"] = 1;
  108. hash["message"] = message;
  109. context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
  110. context.Response.Write(JsonMapper.ToJson(hash));
  111. context.Response.End();
  112. }
  113. public bool IsReusable
  114. {
  115. get
  116. {
  117. return true;
  118. }
  119. }
  120. }