Several Methods:
- Modify the file upload size limit
- Save as a file to the server
- Convert to binary byte stream and save it to database
- 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:
- 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)
- 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.