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