asp.net
您现在的位置: 学网 >> 网站开发 >> NET教程 >> .Net基础 >> 正文
学网教程

NET教程:asp.net 2的文件上传

[ 来源:NET教程 | 作者:NET教程 | 时间:2008-3-4 | 去论坛]

在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];

    

1
学网·特别声明:
本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。本站所有文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题,请联系编辑人员Xababy#Gmail.com,我们尽快予以更正。
设为首页 - 收藏学网 - 关于学网 - RSS订阅 - 版权申明 - 友情链接 - 联系学网 - 网站地图 - 投稿学网
学网·2004-2008版权所有
© CopyRight 2004-2008 WwW.Xue5.CoM.Inc All Rights Reserved
合作、建议、联系::cainiaoo.cn#live.cn QQ:329700200,1103290

学网_致力于提供优质免费的电脑学习教程
陕ICP备05000834号