upload_json.ashx.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections;
  3. using System.Web;
  4. using System.IO;
  5. using System.Globalization;
  6. using Library;
  7. using LitJson;
  8. namespace MySystem.mybjq.asp.net
  9. {
  10. /// <summary>
  11. /// upload_json 的摘要说明
  12. /// </summary>
  13. public class upload_json : IHttpHandler
  14. {
  15. private HttpContext context;
  16. public void ProcessRequest(HttpContext context)
  17. {
  18. String aspxUrl = context.Request.Path.Substring(0, context.Request.Path.LastIndexOf("/") + 1);
  19. //文件保存目录路径
  20. String savePath = "/up/";
  21. //文件保存目录URL
  22. String saveUrl = "/up/";
  23. //定义允许上传的文件扩展名
  24. Hashtable extTable = new Hashtable();
  25. extTable.Add("image", "gif,jpg,jpeg,png,bmp");
  26. extTable.Add("flash", "swf,flv");
  27. extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4");
  28. extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
  29. //最大文件大小
  30. long maxSize = 1073741824000;
  31. this.context = context;
  32. HttpPostedFile imgFile = context.Request.Files["imgFile"];
  33. if (imgFile == null)
  34. {
  35. showError("请选择文件。");
  36. }
  37. String dirPath = context.Server.MapPath(savePath);
  38. if (!Directory.Exists(dirPath))
  39. {
  40. //showError("上传目录不存在。");
  41. Directory.CreateDirectory(dirPath);
  42. }
  43. String dirName = context.Request.QueryString["dir"];
  44. if (String.IsNullOrEmpty(dirName))
  45. {
  46. dirName = "image";
  47. }
  48. if (!extTable.ContainsKey(dirName))
  49. {
  50. showError("目录名不正确。");
  51. }
  52. String fileName = imgFile.FileName;
  53. String fileExt = Path.GetExtension(fileName).ToLower();
  54. if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
  55. {
  56. showError("上传文件大小超过限制。");
  57. }
  58. if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
  59. {
  60. showError("上传文件扩展名是不允许的扩展名。\n只允许" + ((String)extTable[dirName]) + "格式。");
  61. }
  62. //创建文件夹
  63. dirPath += dirName + "/";
  64. saveUrl += dirName + "/";
  65. if (!Directory.Exists(dirPath))
  66. {
  67. Directory.CreateDirectory(dirPath);
  68. }
  69. String ymd = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo);
  70. dirPath += ymd + "/";
  71. saveUrl += ymd + "/";
  72. if (!Directory.Exists(dirPath))
  73. {
  74. Directory.CreateDirectory(dirPath);
  75. }
  76. String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
  77. String filePath = dirPath + newFileName;
  78. imgFile.SaveAs(filePath);
  79. String fileUrl = saveUrl + newFileName;
  80. if (dirName != "media")
  81. {
  82. if (!function.check_pic(filePath))
  83. {
  84. File.Delete(filePath);
  85. context.Response.End();
  86. }
  87. if (function.getImgRule(filePath)["width"] > 2000)
  88. {
  89. String newFileName_sl = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + "_sl" + fileExt;
  90. function.imgcut(2000, filePath, dirPath + newFileName_sl);
  91. File.Delete(filePath);
  92. fileUrl = saveUrl + newFileName_sl;
  93. }
  94. }
  95. Hashtable hash = new Hashtable();
  96. hash["error"] = 0;
  97. hash["url"] = Config.Oss.SourceHost + fileUrl;
  98. MySystem.OssHelper.Instance.ScanQueue(fileUrl, "");
  99. context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
  100. context.Response.Write(JsonMapper.ToJson(hash));
  101. context.Response.End();
  102. }
  103. private void showError(string message)
  104. {
  105. Hashtable hash = new Hashtable();
  106. hash["error"] = 1;
  107. hash["message"] = message;
  108. context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
  109. context.Response.Write(JsonMapper.ToJson(hash));
  110. context.Response.End();
  111. }
  112. public bool IsReusable
  113. {
  114. get
  115. {
  116. return false;
  117. }
  118. }
  119. }
  120. }