How to upload large files in asp.net applications

Source: Internet
Author: User

Often in the project to use a large file upload, to upload the file has more than 100 m, so the study of the domestic use of large file upload components found in more than two controls Aspnetupload 2.0 and Lion.Web.UpLoadModule, two control methods are: using the implied httpworkerrequest, with its getpreloadedentitybody and The Readentitybody method reads data from the pipe that IIS creates for ASP.net. Chris Hynes provides us with a scheme (with HttpModule) that allows you to upload a large file, in real time, to show the upload progress.

Lion.Web.UpLoadModule and Aspnetupload two. NET components are used in this scenario.

When the flyer file, two software methods are the same, inherit 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. Receivedlength = 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 num3 = 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. Receivedlength = 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;
}


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.