欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-09 12:31:41
复制代码 代码如下:if (fileupload1.hasfile) try { fileupload1.saveas("d:\\lucenedata\\" + fileu...
复制代码 代码如下:

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;
}
}
}