Study on uploading large files

Source: Internet
Author: User

ASP. NET upload control V1.0 provided by beta2
Demo: http://www.new57.com/Upload.aspx
Download: http://www.new57.com/download/ewebapp.net.zip

Features

1. Supports uploading large files without refreshing the instant progress display
2. You can set automatic compression or decompression for uploaded files.
3. Custom compression, scaling, and watermarks for image uploads
4. Customize the format and size of the uploaded file
5. automatically save and process files and provide related interfaces
6. Support for verification controls
7. You can customize the control style.
8. The compression operation can append files, compress additional information, and set the compression password.

How to Use

1. Web. config settings
This control uses httpmodule
Therefore, you must add the httpmodule of the control in Web. config.

If you want to upload a large file, you must modify the upload size and timeout of Web. config.

2. Control Reference
Method 1: Use vs to add the control directly in the toolbar and drag and drop the control.
Method 2: Copy ewebapp. Upload. DLL to the bin directory.
3. Use of controls
This control actually contains two controls: showupload and upload.
(1) The showupload control is the control that displays the upload progress.
If you only want to display the upload progress, you can reference the showupload control on the page.
Do not modify the guid value of the control because it is the unique identifier of the upload progress. If it is fixed, multiple users will encounter Errors During simultaneous upload.
Configure one to N htmlupfile controls. The htmlupfile control must contain runat = "server"
You can specify the upload button ID for the submitid control to specify which button will submit the file
You can also use the first button to upload the file and display the progress by default.
You can also set the display style of some controls, or use the default
(2) the upload control is an upload control.
In beta2, the auto-save option is used. If autosave is set to false, you only need to call the Save method of the control to process and save the uploaded files.
The showupload control is built in upload. You can set the built-in progress bar in the "progress bar" setting of the control.
Note the following points for file processing:
The first choice point is the upload type "uptype" setting
Select image to perform the following operations:
Image compression, image scaling, and image watermark
Select File to perform the following operations:
File compression (ZIP), file decompression (unzip), and normal upload (normal)
Note that the compression function currently only supports zipAlgorithm
For more settings and handling instructions, visit my blog

Tips

1. Use the upload control to upload multiple files and display the progress
You only need to set showupload of the first upload control to true, and showupload of other upload controls to false.
The storage path of each file is obtained by the fullpath of the control.
2. Only save the thumbnail
Set uptype to image and set onlysmall to true
The saved path is directly returned by fullpath.
3. Perform a compressed upload and automatically decompress the file so that multiple files can be uploaded at a time.
Set uptype to file and savetype to unzip.
Specify upzipdir as the decompression path.
4. Add a watermark.
Set uptype to image and set waterprint to the watermark image address on the server (leave it blank without watermarks)
5. Save the file to the specified location
By default, the system stores documents in the form of "Upload Folder \ year \ month \ day \ guid \ file name"
If you want to save the file to eupload \ file name, you can set it as follows:
Topath is set to eupload
Leave guid empty
Set usertimesave to false.
We recommend that you do not set filename. The default value is the file name.
If strongsave is set to true, the upload will forcibly overwrite the file with the same file name.
If strongsave is set to false, save the automatically added file (?) Identifier
That is, if an ewebapp. txt file already exists, the File Uploaded with the same name will be saved to ewebapp (1). txt, and so on.
After the upload is complete, filename returns the actually saved file.
Fullpath is the full path relative to the root directory.

---------------------------

Research on uploading large files


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.

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.