当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net 2.0的文件上传(突破上传限制4M)

ASP.NET
AspNetPager与Socut.Data使用实例代码
asp.net JavaScript插件 JavaScript Function Outliner
asp.net for循环语句
asp.net access添加返回自递增id的实现方法
asp.net SAF 中缓存服务的实现
asp.net小孔子cms中的数据添加修改
asp.net自定义控件代码学习笔记
用javascript css实现GridView行背景色交替、鼠标划过行变色,点击行变色选中
.net三层结构初探分析
asp.net+js实时奥运金牌榜代码
asp.net SqlHelper数据访问层的使用
asp.net中利用ashx实现图片防盗链的原理分析
asp.net 2.0多语言网站解决方法
Ajax.net Sys未定义错误解决办法
.dll 文件反编译的工具软件集合
asp.net gridview 72般绝技
常用的在数据库中建立无限级树形菜单的asp.net代码
asp.net生成静态页并分页+ubb
Asp.net 通用万级数据分页代码[修正下载地址]
较为全面的Asp.net提交验证方案分析 (上)

ASP.NET 中的 asp.net 2.0的文件上传(突破上传限制4M)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-11   浏览: 45 ::
收藏到网摘: n/a

在asp.net 2.0中,因为有了fileupload控件,上传文件十分简单
复制代码 代码如下:

if (FileUpload1.HasFile)
try
{
FileUpload1.SaveAs("d:\\luceneData\\" + FileUpload1.FileName);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUpload1.PostedFile.ContentType;
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}
else
{
Label1.Text = "You have not specified a file.";
}
还可以在web.config文件中,突破默认上传限制的4MB,比如
<httpRuntime
executionTimeout="110"
maxRequestLength="11000"
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" />
设置maxRequestLenth属性,这里为11000KB,即11MB。
而对于多文件上传,也很简单,比如一个例子
string filepath = "d:\\luceneData\\";
HttpFileCollection uploadedFiles = Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
try
{
if (userPostedFile.ContentLength > 0)
{
Label1.Text += "<u>File #" + (i + 1) +
"</u><br>";
Label1.Text += "File Content Type: " +
userPostedFile.ContentType + "<br>";
Label1.Text += "File Size: " +
userPostedFile.ContentLength + "kb<br>";
Label1.Text += "File Name: " +
userPostedFile.FileName + "<br>";
userPostedFile.SaveAs(filepath + "\\" +
System.IO.Path.GetFileName(userPostedFile.FileName));
Label1.Text += "Location where saved: " +
filepath + "\\" +
System.IO.Path.GetFileName(userPostedFile.FileName) +
"<p>";
}
}
catch (Exception Ex)
{
Label1.Text += "Error: <br>" + Ex.Message;
}
}
}