[Net] ASP. NET large file upload Research

Source: Internet
Author: User


Research on ASP. NET large file upload
 

 

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;
}

 

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.