在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;
}
}
}
新闻热点
疑难解答
图片精选