网站首页 > 网络编程教程 > ASP.NET教程 > asp.net 2的文件上传

asp.net 2的文件上传

  • 作者:互联网
  • 时间:2009-07-01 15:54:04

在asp.net 2.0中,上传文件时变的比较方便了,因为有了fileupload控件,使用十分简单,
if (Fi***pload1.HasFile)
            try
            {
                Fi***pload1.SaveAs("d:\luceneData\" + Fi***pload1.FileName);
                La***1.Text = "File name: " +
                     Fi***pload1.PostedFile.FileName + "
" +
                     Fi***pload1.PostedFile.ContentLength + " kb
" +
                     "Content type: " +
                     Fi***pload1.PostedFile.ContentType;
            }
            catch (Exception ex)
            {
                La***1.Text = "ERROR: " + ex***ssage.ToString();
            }
        else
        {
            La***1.Text = "You have not specified a file.";
        }
还可以在we***onfig文件中,突破默认上传限制的4MB,比如
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 = Re***st.Files;

        for (int i = 0; i < up***dedFiles.Count; i++)
        {
            HttpPostedFile userPostedFile = uploadedFiles[i];

            try
            {
                if (us***ostedFile.ContentLength > 0)
                {
                    La***1.Text += "File #" + (i + 1) +
                       "

";
                    La***1.Text += "File Content Type: " +
                       us***ostedFile.ContentType + "
";
                    La***1.Text += "File Size: " +
                       us***ostedFile.ContentLength + "kb
";
                    La***1.Text += "File Name: " +
                       us***ostedFile.FileName + "
";

                    us***ostedFile.SaveAs(filepath + "\" +
                       Sy***m.IO.Path.GetFileName(us***ostedFile.FileName));

                    La***1.Text += "Location where saved: " +
                       filepath + "\" +
                       Sy***m.IO.Path.GetFileName(us***ostedFile.FileName) +
                       "

";
                }
            }
            catch (Exception Ex)
            {
                La***1.Text += "Error:
" + Ex***ssage;
            }
        }

    }


http://www.cnblogs.com/jackyrong/archive/2006/09/26/514969.html