aspx接受aspx页面的文件很简单,用HtmlInputFile,就可以了,但是如果接受Html页面post的文件
就不怎么好办了,我仿照asp的方法做法如下,自己测试通过,拿出来给大家共享,可以限制
文件内容,类型,大小,自定义存储位置,在congfig.xml内
html页的内容:(来自FCKeditor)
1<html>
2<head>
3<title>FCKeditor - Uploaders Tests</title>
4<script language="javascript">
5
6function SendFile()
7{
8var sUploaderUrl = cmbUploaderUrl.value ;
9
10if ( sUploaderUrl.length == 0 )
11sUploaderUrl = txtCustomUrl.value ;
12
13if ( sUploaderUrl.length == 0 )
14{
15alert( 'Please provide your custom URL or select a default one' ) ;
16return ;
17}
18
19eURL.innerHTML = sUploaderUrl ;
20txtUrl.value = '' ;
21
22frmUpload.action = sUploaderUrl ;
23frmUpload.submit() ;
24}
25
26function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg )
27{
28switch ( errorNumber )
29{
30case 0 : // No errors
31txtUrl.value = fileUrl ;
32alert( 'File uploaded with no errors' ) ;
33break ;
34case 1 : // Custom error
35alert( customMsg ) ;
36break ;
37case 10 : // Custom warning
38txtUrl.value = fileUrl ;
39alert( customMsg ) ;
40break ;
41case 201 :
42txtUrl.value = fileUrl ;
43alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
44break ;
45case 202 :
46alert( 'Invalid file' ) ;
47break ;
48case 203 :
49alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ;
50break ;
51default :
52alert( 'Error on file upload. Error number: ' + errorNumber ) ;
53break ;
54}
55}
56
57</script>
58</head>
59<body>
60<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
61<tr>
62<td>
63<table border="0" cellpadding="0" cellspacing="0" width="100%">
64<tr>
65<td nowrap="" style="height: 43px">
66Select the "File Uploader" to use:<br/>
67<select id="cmbUploaderUrl" name="Select1">
68<option selected="" value="asp/upload.asp">ASP</option>
69<option value="php/upload.php">PHP</option>
70<option value="Upload.aspx?type=image">aspx</option>
71</select>
72</td>
73<td nowrap="" style="height: 43px"> </td>
74<td style="height: 43px" width="100%">
75Custom Uploader URL:<br/>
76<input disabled="" id="txtCustomUrl" style="WIDTH: 100%; BACKGROUND-COLOR: #dcdcdc" type="text"/>
77</td>
78</tr>
79</table>
80<br/>
81<table border="0" cellpadding="0" cellspacing="0" width="100%">
82<tr>
83<td nowrap="">
84<form action="" enctype="multipart/form-data" id="frmUpload" method="post" target="UploadWindow">
85Upload a new file:<br/>
86<input name="NewFile" type="file"/><br/>
87<input onclick="SendFile();" type="button" value="Send it to the Server"/>
88</form>
89</td>
90<td style="WIDTH: 16px"> </td>
91<td valign="top" width="100%">
92Uploaded File URL:<br/>
93<input id="txtUrl" readonly="" style="WIDTH: 100%" type="text"/>
94</td>
95</tr>
96</table>
97<br/>
98Post URL: <span id="eURL"> </span>
99</td>
100</tr>
101<tr>
102<td height="100%">
103<iframe height="100%" name="UploadWindow" width="100%"></iframe>
104</td>
105</tr>
106</table>
107</body>
108</html>
upload.aspx的内容:
1@ Page Language="C#" AutoEventWireup="true" CodeFile="Upload.aspx.cs" Inherits="Upload"
下面是后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Collections.Specialized;
public partial class Upload : System.Web.UI.Page
{
public void SendResults(int errorNumber, string fileUrl, string fileName, string customMsg)
{
StringBuilder text = new StringBuilder();
text.Append("
1<script type='\"text/javascript\"'>");
2text.Append("window.parent.OnUploadCompleted(" + errorNumber + ",\"" + fileUrl.Replace("\"", "\\\\\"") + "\",\"" + fileName.Replace("\"", "\\\\\"") + "\",\"" + customMsg.Replace("\"", "\\\\\"") + "\") ;\n");
3text.Append(" </script>
");
Response.Write(text.ToString());
Response.End();
}
public void GetConfig(string type, out string[] allowedExt, out string[] denyedExt,out string savePath,out long maxSize)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath(@".\Config.xml"));
XmlElement root=doc.DocumentElement;
XmlNodeList imageNodelist=root.GetElementsByTagName(type);
allowedExt = imageNodelist[0].FirstChild.InnerText.Trim().Split('|');
denyedExt = imageNodelist[0].LastChild.InnerText.Trim().Split('|');
savePath = root.GetElementsByTagName("userPath").Item(0).InnerText.Trim();
try
{
maxSize = Convert.ToInt64(root.GetElementsByTagName("maxSize").Item(0).InnerText.Trim());
}
catch { maxSize = 10*1024; }
}
protected void Page_Load(object sender, EventArgs e)
{
string[] allowedExt = new string[] { }, denyedExt = new string[] { };
string savePath = String.Empty;
long maxSize = 10000;
string type = Request.QueryString["Type"];
if(type!=null&&type!=string.Empty)
type=type.ToLower();
else
type="file";
if (type == "image")
{
GetConfig("image", out allowedExt, out denyedExt, out savePath,out maxSize);
}
if (type == "file")
{
GetConfig("file", out allowedExt, out denyedExt, out savePath, out maxSize);
}
if (type == "flash")
{
GetConfig("flash", out allowedExt, out denyedExt, out savePath, out maxSize);
}
if (savePath == string.Empty||savePath=="")
savePath = "~/UserFiles/";
if(!savePath.EndsWith("/"))savePath+="/";
本新闻共 2 页,当前在第 1 页 1 2