ASP.NET 2.0(C#)使用控件FileUpload实现文件上传:
上传代码:
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath(".")+"/"+FileUpload1.FileName);
Label1.Text = "文件名称: " +FileUpload1.PostedFile.FileName +
"<br>文件大小:" + FileUpload1.PostedFile.ContentLength/1024 +
" KB<br>文件类型:" + FileUpload1.PostedFile.ContentType+
"<br>文件上传路径:"+Server.MapPath(".")+"/"+FileUpload1.FileName.;
}
默认情况下,使用 FileUpload 控件上载到服务器的文件最大为 4MB 左右。
要更改大小限制,可以在 web.config.comments 文件(可以在 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG 的 ASP.NET 2.0 配置文件夹中找到)或应用程序的 web.config 文件中进行一些改动。
在 web.config.comments 文件中,查找一个名为 <executionTimeout>的节点
<httpRuntime
executionTimeout="110"
maxRequestLength="4096"
requestLengthDiskThreshold="80"
useFullyQualifiedRedirectUrl="false"
minFreeThreads="8"
minLocalRequestFreeThreads="4"
appRequestQueueLimit="5000"
enableKernelOutputCache="true"
enableVersionHeader="true"
requireRootedSaveASPath="true"
enable="true"
shutdownTimeout="90"
delayNotificationTimeout="5"
waitChangeNotification="0"
maxWaitChangeNotification="0"
enableHeaderChecking="true"
sendCacheControlHeader="true"
apartmentThreading="false" />
负责上载文件大小的设置是 maxRequestLength 属性。
根据扩展名获取文件类型:
string fileExt =System.IO.Path.GetExtension(FileUpload1.FileName);
文件扩展名是 . XXX,如 . EXE