config.xml

Code
<?xml version="1.0" encoding="utf-8" ?>
<UploadConfig>
<HttpFileExtensions>.7z,.aiff,.asf,.avi,.bmp,.csv,.doc,.fla,.flv,.gif,.gz,.gzip,.jpeg,.jpg,.mid,
.mov,.mp3,.mp4,.mpc,.mpeg,.mpg,.ods,.odt,.pdf,.png,.ppt,.pxd,.qt,.ram,.rar,.rm,.rmi,
.rmvb,.rtf,.sdc,.sitd,.swf,.sxc,.sxw,.tar,.tgz,.tif,.tiff,.txt,.vsd,.wav,.wma,.wmv,.xls,.xml,.zip,
.bmp,.gif,.jpg,.png,.swf,.flv.aiff,.asf,.avi,.bmp,.fla,.flv,.gif,.jpeg,.jpg,.mid,.mov,.mp3,.mp4,
.mpc,.mpeg,.mpg,.png,.qt,.ram,.rm,.rmi,.rmvb,.swf,.tif,.tiff,.wav,.wma,.wmv</HttpFileExtensions>
<HttpFilePath>/IFEditor/UploadFile</HttpFilePath>
<HttpFileContentLength>4194304</HttpFileContentLength>
</UploadConfig>
Upload.aspx.cs

Code
1 using System;
2 using System.Collections.Generic;
3 using System.Web;
4 using System.Web.UI;
5 using System.Web.UI.WebControls;
6 using System.IO;
7 using System.Configuration;
8 using System.Xml;
9
10 public partial class DTEditor_Upload_Upload : System.Web.UI.Page
11 {
12 //===================================================================
13 // 私有上传类型XPath变量
14 //===================================================================
15
16 #region 私有变量定义上传类型XPATH
17 private string XPathFileExtensions = "UploadConfig/HttpFileExtensions";
18
19 private string XPathFilePath = "UploadConfig/HttpFilePath";
20
21 private string XPathFileContentLength = "UploadConfig/HttpFileContentLength";
22 #endregion
23
24 #region 上传内容限定
25 private string[] x_FileExtensions = null;
26 private string x_FilePath = "";
27 private int x_FileContentLength = 0;
28 #endregion
29
30 private string lpResultError = string.Empty;
private string lpResultMessage = string.Empty;
31
32 protected void Page_Load(object sender, EventArgs e)
33 {
34 if ((Request.Url.Host != ConfigurationManager.AppSettings["IFEditorAccess"])
|| Request.Files.Count < 1)
{
Response.Write("{error:'未授权访问',msg:'" + lpResultMessage + "'}");
return;
} // 禁用未授权访问
35
36 XmlDocument xmlDoc = new XmlDocument();
37 try
38 {
39 xmlDoc.Load(Server.MapPath("config.xml"));
40
41 x_FileExtensions = xmlDoc.SelectSingleNode(XPathFileExtensions).InnerText.Split(",".ToCharArray());
42
43 x_FilePath = xmlDoc.SelectSingleNode(XPathFilePath).InnerText;
44
45 x_FileContentLength = Int32.Parse(xmlDoc.SelectSingleNode(XPathFileContentLength).InnerText);
46 }
47 catch (XmlException ex)
{
Response.Write("{error:'" + ex.Message + "',msg:'" + lpResultMessage + "'}");
return;
}
48 catch (Exception ex)
{
Response.Write("{error:'" + ex.Message + "',msg:'" + lpResultMessage + "'}");
return;
}
49 try
50 {
51 bool CanUploadFile = false;
52
53 for (int i = 0; i < x_FileExtensions.Length; i++)
54 {
55 if (Path.GetExtension(Request.Files[0].FileName) == x_FileExtensions[i])
{
CanUploadFile = true; break;
}
56 }
57
58 if (!CanUploadFile)
{
lpResultError = string.Format(string.Format("不允许上传 {0} 格式文件",
Path.GetExtension(Request.Files[0].FileName)));
}
59 else
60 {
61 if (Request.Files[0].ContentLength > x_FileContentLength - 1)
{
CanUploadFile = false;
lpResultError = string.Format("文件大小超过 {0} KB 限制", x_FileContentLength / 1024);
}
62 else
63 {
64 if (CanUploadFile)
65 {
66 string lpFileName = x_FilePath.Substring(x_FilePath.Length - 1, 1) == "/" ? "{0}{1}{2}" : "{0}/{1}{2}";
67
68 lpFileName = string.Format(lpFileName, x_FilePath,
DateTime.Now.ToString("yyyyMMddHHmmssfff"),
Path.GetExtension(Request.Files[0].FileName));
69
70 Request.Files[0].SaveAs(Server.MapPath(lpFileName));
lpResultMessage = lpFileName;
71 }
72 }
73 }
74 }
75 catch (IOException ex) { lpResultError = ex.Message; }
76 catch (Exception ex) { lpResultError = ex.Message; }
77
78 Response.Write("{error:'" + lpResultError + "',msg:'" + lpResultMessage + "'}");
Response.End();
79 }
80 }
81
default.html

Code
<script src="IFEditor/Upload/UploadFile.js" type="text/javascript"></script>
<input type="file" id="AjaxFileUpload" name="AjaxFileUpload" accept="jpg" /> <input type="button" value="上传" onclick="return AjaxFileUpload('AjaxFileUpload','/Upload/Upload.aspx','Results');" />
<input type="text" id="Results" />
点击下载 UploadFile.js 请下载后改名为 js文件