Asp.net large file upload Solution

Source: Internet
Author: User

In this project, a large file upload is used, and the number of files to be uploaded is more than 100 MB.
The component found that many of them used two controls: aspnetupload 2.0 and Lion. Web. uploadmodule. In addition, they thought about its blog.
The way mentioned in http://blog.joycode.com/saucer/archive/2004/03/16/16225.aspx
The two controls use the implicit httpworkerrequest method to read data from the pipe created for ASP. NET by using its getpreloadedentitybody and readentitybody method. Chris Hynes provides us with such a solution (using httpmodule) that allows you to upload large files and display the upload progress in real time.
Lion. Web. uploadmodule and aspnetupload both use this solution.
When the leaflet file is used, the two software methods are the same, inheriting the httpmodule
Httpapplication application1 = sender as httpapplication;
Httpworkerrequest request1 = (httpworkerrequest) (iserviceprovider) httpcontext. Current). getservice (typeof (httpworkerrequest ));
Try
{
If (application1.context. Request. contenttype. indexof ("multipart/form-Data") <=-1)
{
Return;
}
// Check the hasentitybody
If (! Request1.hasentitybody ())
{
Return;
}

Int num1 = 0;
Timespan span1 = datetime. Now. Subtract (this. begintime );

String text1 = application1.context. Request. contenttype. tolower ();

Byte [] buffer1 = encoding. ASCII. getbytes ("\ r \ n --" + text1.substring (text1.indexof ("boundary =") + 9). tochararray ());
Int num2 = convert. toint32 (request1.getknownrequestheader (11 ));
Progress progress1 = new progress ();

Application1.context. Items. Add ("filelist", new hashtable ());

Byte [] buffer2 = request1.getpreloadedentitybody ();
Num1 + = buffer2.length;

String text2 = This. analysepreloadedentitybody (buffer2, "uploadguid ");
If (text2! = String. Empty)
{
Application1.context. Items. Add ("lionsky_uploadmodule_uploadguid", text2 );
}
Bool flag1 = true;
If (num2> This. uploadfilelength () & (0> span1.totalhours) | (span1.totalhours> 3 )))
{
Flag1 = false;
}
If (0> span1.totalhours) | (span1.totalhours> 3 ))
{
Flag1 = false;
}
String text3 = This. analysepreloadedentitybody (buffer2, "uploadfolder ");
Arraylist list1 = new arraylist ();
Requeststream stream1 = new requeststream (buffer2, buffer1, null, requeststream. filestatus. Close, requeststream. readstatus. noread, text3, flag1, application1.context, String. Empty );
List1.addrange (stream1.readbody );
If (text2! = String. Empty)
{
Progress1.filelength = num2;
Progress1.ededlength = num1;
Progress1.filename = stream1.originalfilename;
Progress1.filecount = (hashtable) application1.context. items ["filelist"]). count;
Application1.application ["_ uploadguid _" + text2] = progress1;
}
If (! Request1.isentireentitybodyispreloaded ())
{
Byte [] buffer4;
Arraylist list2;
Int Number3 = 204800;
Byte [] buffer3 = new byte [num3];
While (num2-num1)> = num3)
{
If (! Application1.context. response. isclientconnected)
{
This. clearapplication (application1 );
}
Num3 = request1.readentitybody (buffer3, buffer3.length );
Num1 + = num3;
List2 = stream1.contentbody;
If (list2.count> 0)
{
Buffer4 = new byte [list2.count + buffer3.length];
List2.copyto (buffer4, 0 );
Buffer3.copyto (buffer4, list2.count );
Stream1 = new requeststream (buffer4, buffer1, stream1.filestream, stream1.fstatus, stream1.rstatus, text3, flag1, application1.context, stream1.originalfilename );
}
Else
{
Stream1 = new requeststream (buffer3, buffer1, stream1.filestream, stream1.fstatus, stream1.rstatus, text3, flag1, application1.context, stream1.originalfilename );
}
List1.addrange (stream1.readbody );
If (text2! = String. Empty)
{
Progress1.ededlength = num1;
Progress1.filename = stream1.originalfilename;
Progress1.filecount = (hashtable) application1.context. items ["filelist"]). count;
Application1.application ["_ uploadguid _" + text2] = progress1;
}
}
Buffer3 = new byte [num2-num1];
If (! Application1.context. response. isclientconnected & (stream1.fstatus = requeststream. filestatus. Open ))
{
This. clearapplication (application1 );
}
Num3 = request1.readentitybody (buffer3, buffer3.length );
List2 = stream1.contentbody;
If (list2.count> 0)
{
Buffer4 = new byte [list2.count + buffer3.length];
List2.copyto (buffer4, 0 );
Buffer3.copyto (buffer4, list2.count );
Stream1 = new requeststream (buffer4, buffer1, stream1.filestream, stream1.fstatus, stream1.rstatus, text3, flag1, application1.context, stream1.originalfilename );
}
Else
{
Stream1 = new requeststream (buffer3, buffer1, stream1.filestream, stream1.fstatus, stream1.rstatus, text3, flag1, application1.context, stream1.originalfilename );
}
List1.addrange (stream1.readbody );
If (text2! = String. Empty)
{
Progress1.receivedlength = num1 + buffer3.length;
Progress1.filename = stream1.originalfilename;
Progress1.filecount = (hashtable) application1.context. items ["filelist"]). count;
If (flag1)
{
Progress1.uploadstatus = progress. uploadstatusenum. Uploaded;
}
Else
{
Application1.application. Remove ("_ uploadguid _" + text2 );
}
}
}
Byte [] buffer5 = new byte [list1.count];
List1.copyto (buffer5 );
This. populaterequestdata (request1, buffer5 );
}
Catch (exception exception1)
{
This. clearapplication (application1 );
Throw exception1;
}

The method mentioned by the author can also upload large files using mime, and download it at the following address:
Http://krystalware.com/files/slickupload.zip
However, it is much easier to think about it.

RelatedArticle:
Make the default Upload Component of Asp.net support progress bar reflection
Http://blog.joycode.com/dotey/archive/2005/06/12/53557.aspx // Baoyu
Http://blog.joycode.com/saucer/archive/2004/03/16/16225.aspx // Si Gui
Http://www.cnblogs.com/bestcomy/archive/2004/06/09/14267.aspx // bestcomy
Http://krystalware.com/wiki/default.aspx/KrystalWiki.UploadSpike1
Http://www.blueidea.com/tech/program/2005/2997.asp // FTP
Upload and download files through Web Services
Http://dotnet.aspx.cc/ShowDetail.aspx? Id = 6381bd5f-51f3-4339-4239-1328564a1b2a
How does the Upload Component not be restricted by setTimeout?
Http://www.ietf.org/rfc/rfc1867.txt? Number = 1867

Download
Http://support.microsoft.com/default.aspx? SCID = KB; en-US; 812406 & Product = ASPnet

Related Article

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.