asp.net upload Big file solution _ practical Skills

Source: Internet
Author: User
Tags file upload

This time in the project, the use of large file upload, to upload the file has more than 100 m, so the study of the current domestic use of large files uploaded
Component discovery uses more than two controls Aspnetupload 2.0 and Lion.Web.UpLoadModule, in addition to Shi in its blog
The way the Don says http://blog.joycode.com/saucer/archive/2004/03/16/16225.aspx
The method of two controls is to read data in chunks from the pipe created by IIS for asp.net, using implicit httpworkerrequest and its getpreloadedentitybody and Readentitybody methods. 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;
}


And Shi said the method using MIME can also upload large files, download at the following address
Http://krystalware.com/files/slickupload.zip
But it's a lot easier to think of Shi methods.

Related articles:
Let the asp.net default upload component 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//Shi
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
Uploading and downloading files through Web services
Http://dotnet.aspx.cc/ShowDetail.aspx?id=6381BD5F-51F3-4339-4239-1328564A1B2A
How is the upload component not subject to settimeout restrictions?
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.