imageUp.ashx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <%@ WebHandler Language="C#" Class="imageUp" %>
  2. <%@ Assembly Src="Uploader.cs" %>
  3. using System;
  4. using System.Web;
  5. using System.IO;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. public class imageUp : IHttpHandler
  9. {
  10. public void ProcessRequest(HttpContext context)
  11. {
  12. context.Response.ContentEncoding = System.Text.Encoding.UTF8;
  13. //上传配置
  14. string pathbase = "upload/"; //保存路径
  15. int size = 10; //文件大小限制,单位mb //文件大小限制,单位KB
  16. string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" }; //文件允许格式
  17. string callback = context.Request["callback"];
  18. string editorId = context.Request["editorid"];
  19. //上传图片
  20. Hashtable info;
  21. Uploader up = new Uploader();
  22. info = up.upFile(context, pathbase, filetype, size); //获取上传状态
  23. string json = BuildJson(info);
  24. context.Response.ContentType = "text/html";
  25. if (callback != null)
  26. {
  27. context.Response.Write(String.Format("<script>{0}(JSON.parse(\"{1}\"));</script>", callback, json));
  28. }
  29. else
  30. {
  31. context.Response.Write(json);
  32. }
  33. }
  34. public bool IsReusable
  35. {
  36. get
  37. {
  38. return false;
  39. }
  40. }
  41. private string BuildJson(Hashtable info)
  42. {
  43. List<string> fields = new List<string>();
  44. string[] keys = new string[] { "originalName", "name", "url", "size", "state", "type" };
  45. for (int i = 0; i < keys.Length; i++)
  46. {
  47. fields.Add(String.Format("\"{0}\": \"{1}\"", keys[i], info[keys[i]]));
  48. }
  49. return "{" + String.Join(",", fields) + "}";
  50. }
  51. }