不会有问题啊。
下面是我测试的结果:Uploaded file Length=9299138 HtmlInputFile Sample
Select File to Upload:
Save as filename (no path): File uploaded successfully to C:\WINNT\TEMP\aaa.zip on the web server
下面是代码:
<%@ Import Namespace="System.IO" %>
<html>
<head> <script language="C#" runat="server"> void Button1_Click(object Source, EventArgs e) { if (Text1.Value == "") {
Span1.InnerHtml = "Error: you must enter a file name";
return;
} if (File1.PostedFile != null) {
int fileLength = File1.PostedFile.ContentLength;
Response.Write("Length="+fileLength);
string filepath = Path.Combine(Path.GetTempPath(), Path.GetFileName(Text1.Value)); try {
File1.PostedFile.SaveAs(filepath);
Span1.InnerHtml = "File uploaded successfully to <b>" + filepath + "</b> on the web server";
}
catch (Exception exc) {
Span1.InnerHtml = "Error saving file <b>" + filepath + "</b><br>"+ exc.ToString();
}
}
} </script></head>
<body> <h3><font face="Verdana">HtmlInputFile Sample</font></h3> <form enctype="multipart/form-data" runat="server"> Select File to Upload: <input id="File1" type=file runat="server"> <p>
Save as filename (no path): <input id="Text1" type="text" runat="server"> <p>
<span id=Span1 style="font: 8pt verdana;" runat="server" /> <p>
<input type=button id="Button1" value="Upload" OnServerClick="Button1_Click" runat="server"> </form></body>
</html>
下面是我测试的结果:Uploaded file Length=9299138 HtmlInputFile Sample
Select File to Upload:
Save as filename (no path): File uploaded successfully to C:\WINNT\TEMP\aaa.zip on the web server
下面是代码:
<%@ Import Namespace="System.IO" %>
<html>
<head> <script language="C#" runat="server"> void Button1_Click(object Source, EventArgs e) { if (Text1.Value == "") {
Span1.InnerHtml = "Error: you must enter a file name";
return;
} if (File1.PostedFile != null) {
int fileLength = File1.PostedFile.ContentLength;
Response.Write("Length="+fileLength);
string filepath = Path.Combine(Path.GetTempPath(), Path.GetFileName(Text1.Value)); try {
File1.PostedFile.SaveAs(filepath);
Span1.InnerHtml = "File uploaded successfully to <b>" + filepath + "</b> on the web server";
}
catch (Exception exc) {
Span1.InnerHtml = "Error saving file <b>" + filepath + "</b><br>"+ exc.ToString();
}
}
} </script></head>
<body> <h3><font face="Verdana">HtmlInputFile Sample</font></h3> <form enctype="multipart/form-data" runat="server"> Select File to Upload: <input id="File1" type=file runat="server"> <p>
Save as filename (no path): <input id="Text1" type="text" runat="server"> <p>
<span id=Span1 style="font: 8pt verdana;" runat="server" /> <p>
<input type=button id="Button1" value="Upload" OnServerClick="Button1_Click" runat="server"> </form></body>
</html>
<httpRuntime
executionTimeout="90"
maxRequestLength="4096" //上传文件大小(k)
useFullyQualifiedRedirectUrl="false"
minFreeThreads="8"
minLocalRequestFreeThreads="4"
appRequestQueueLimit="100" />
把4096改的大点
,默认情况下,asp.net允许4MB的文件上传。在machine.config中有下面的配置:
<httpRuntime
executionTimeout="90"
maxRequestLength="4096"
useFullyQualifiedRedirectUrl="false"
minFreeThreads="8"
minLocalRequestFreeThreads="4"
appRequestQueueLimit="100"
/>
可以修改里面maxRequestLength的大小来改变上传文件的最大值!
因为点击上载按钮时,出错在该页面程序的InitializeComponent()函数之前就发生了,即页面没有初始化时就出现抛出一个异常了;因为我没有用事件跟踪,也不知道异常在哪里出现。
差了一下关于HttpPostedFile的资料:
HttpPostedFile 提供属性和方法以获取关于个别文件的信息以及读取和保存文件。文件以 MIME 多部分/窗体数据格式上载,并且缓存在服务器内存中,直到显式保存到磁盘上。
现在假设文件在上载到服务器内存的时候出错,如果假设成立,那文件生在服务器内存什么时候发生呢?
var fso, f, s;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.GetFolder(filespec);
s = f.Name + " uses " + f.size + " bytes.";
return(s);
}再在客户端上传的那个按钮的Attributes["OnClick"] = if (GetSize(filespec > xxx) return false;
下载时是文件名是中文怎么处理,如response.addHeader("Content-Disposition","attachment; filename="+filename)?
如果文件名是中文的话,到了client文件名就是一堆乱码?
Server.UrlEncode(filename)不行啊,到了clinet就是%2%7之类的乱码,比较原来还乱。
http://expert.csdn.net/expert/Topicview1.asp?id=1210070
楼主的问题怎样解决呢?
所以我看来只能设置最大了,但是这样就使得用户不管多大都能传了,当很大的时候明显时间太长,我刚才试了一下,上传的17兆的东西用了7分钟!关注,请大家继续。
http://expert.csdn.net/Expert/topic/1465/1465648.xml?temp=.3298303
string filename = fileToUpload.PostedFile.FileName;
string togo = path + "/" + filename.Remove(0, filename.LastIndexOf("\\") + 1);
fileToUpload.PostedFile.SaveAs(Server.MapPath(togo)); // get size of file with appropriate expression of size
long fileSize = fileToUpload.PostedFile.ContentLength;
string fileSizeStr;
if(fileSize > 1000000) fileSizeStr = fileSize/1000000 + " Mb";
else if(fileSize > 1000) fileSizeStr = fileSize/1000 + " Kb";
else fileSizeStr = fileSize + " b";
判断一下,超过你要求的字节就不上传不就得拉
过会儿我给你们,以前我也遇上这个问题,只要简单设一下就可以了
<system.web>
<httpRuntime
executionTimeout="想设的超时时间"
maxRequestLength="xxxx"
useFullyQualifiedRedirectUrl="false"
</system.web></configuration>//注maxRequestLength="xxxx"这个里面你可以设大小,以KB为单位
这样就一切ok了。