ASP. NET-Uploading large files (page timeout)

Source: Internet
Author: User

Several Methods:

      1. Modify the file upload size limit
      2. Save as a file to the server
      3. Convert to binary byte stream and save it to database
      4. Write binary data to the disk in a loop

I. Modify the file upload size limit

Modify the Web. config and machine. config files.

Modify web. config in the root directory of the website:

Original:

<Httpruntime executiontimeout ="90"Maxrequestlength ="800000"Usefullyqualifiedredirecturl ="False"/>

Modify executiontimeout to modify the time-out period. Modify maxrequestlength to modify the maximum number of uploaded files.

Modify Microsoft. NET \ framework \ v1.1.4322 \ config \ machine. config on the machine:

Original:

<Httpruntime executiontimeout ="90"Maxrequestlength ="4096"Usefullyqualifiedredirecturl ="False"Minfreethreads ="8"Minlocalrequestfreethreads ="4"Apprequestqueuelimit ="100"Enableversionheader ="True"/>

Modify executiontimeout to modify the time-out period. Modify maxrequestlength to modify the maximum number of uploaded files.

You can also (not tested, do not know if it is useful ):

Disable the IIS Admin Service in "service.
Find the metabase. xml file under c: \ windows \ system32 \ inetsrv.
Locate aspmaxrequestentityallowed and change it to the expected value (you can change it to 20 m or 20480000)
Save the disk and restart the IIS Admin Service.

 

Prerequisites:

Private StringGetfilepath (){StringFolder = server. mappath ("Temp");StringFile = fileupload1.filename;StringRealfile = folder +"\\"+ File;ReturnRealfile ;}
 

2. Save the file to the server

Protected VoidUpload_click (ObjectSender, eventargs e ){StringFilename = fileupload1.filename; fileupload1.saveas (server. mappath ("Source/") + Filename );}
 
Protected VoidButton4_click (ObjectSender, eventargs e) {httppostedfile posted = fileupload1.postedfile;StringRealfile = getfilepath (); posted. saveas (realfile );}
Protected VoidButton#click (ObjectSender, eventargs e ){StringFilepath = getfilepath ();Byte[] B = fileupload1.filebytes; filestream FS =NewFilestream (filepath, filemode. Create); FS. Write (B, 0, B. Length); FS. Close ();}

Iii. convert to a binary byte stream and save it to the database

Protected VoidButton3_click (ObjectSender, eventargs e) {httppostedfile posted = fileupload1.postedfile;Byte[] B =New Byte[Posted. contentlength]; posted. inputstream. Read (B, 0, B. Length );/** Write data to the database */}

 

4. Write binary data to the disk in a loop

     Protected   Void Button3_click ( Object Sender, eventargs e ){ String Filepath = getfilepath (); filestream stream =New Filestream (filepath, filemode. Create ); Int Length = 20480; Byte [] B = New   Byte [Length]; Try { Int Size = fileupload1.postedfile. inputstream. Read (B, 0, length ); While (Size> 0) {stream. Write (B, 0, size); size = fileupload1.postedfile. inputstream. Read (B, 0, length );}} Catch (Exception ex) {response. Write (ex. Message +" <Br/> "); Response. Write (ex. innerexception. Message );} Finally { If (Stream! = Null ) {Stream. Flush (); stream. Close ();}}}

 

Appendix:

  1. Increase the script timeout time in IIS by clicking the "Configure" button under the "home directory" of the "site or virtual directory" in IIS. Set the script timeout time: 300 seconds (Note: not the Session Timeout time)
  2. Solve the problem that server 2003 cannot download attachments larger than 4 MB
    Disable the IIS Admin Service in "service.
    Find the metabase. xml file under windows \ system32 \ inetsrv.
    Locate aspbufferinglimit and change it to the expected value (20 m or 20480000)
    Save the disk and restart the IIS Admin Service.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.