ASP. NET large File Upload Component Development Summary

Source: Internet
Author: User

ASP. NET large File Upload Component Development Summary (6) --- instructions for using the large File Upload Component

1. Writing Purpose
To facilitate the use of large File Upload components, close-up instructions for use to specify the interface for calling components, operation procedures and precautions.

2 Background
Because ASP. NET loads all files into the server memory during file uploading, it consumes a lot of server resources and cannot display the upload progress.

Because there are many resources in the server, the size of files uploaded by ASP. NET is limited. This component solves all these problems

3 Definition
Large file: the size of a large file is not limited,

Web. config: the configuration file of ASP. NET.

4 Functions
You can use this component to upload files using ASP. NET. This component can remove ASP. NET restrictions on the size of the uploaded files and provide a prompt on the upload progress,

You can also choose to interrupt the upload process. Reduce the consumption of server resources (tested, server resource consumption is almost the same as running a common ASP. NET page)

5. Main Components and operations:
5.1 bigfileuploadmodulehandle
The large file upload processing module is used to analyze the request content after a client request is sent to the server. If a file is uploaded, IIS sends the data to the ASP. NET

When the channel is used, the system reads data, writes data to the file, and re-organizes the request content. The re-organized content removes the request content of the file content. After receiving the request

The upload control cannot read the file content, and the attributes of other components can be accessed normally.

5.2 datareader
Request data analysis and file writing.

5.3 upfileinfo
The property structure of the uploaded file for internal use by the component.

5.4 uploadcontext
This class is mainly used in the file upload environment when components are used. Specific methods and attributes are as follows:

Uploadcontext: constructor. You need to input the object on the file upload page and the temporary file storage directory. Some constructors cannot be called externally. To build a file upload environment,

Call the static method provided by uploadcontextfactory

Tmepfiledir: obtains and sets the path for storing uploaded temporary files.

Filenames: List of uploaded file names

Total length of information sent by totallength, including the request content of all controls on the page

Readedlength received information length includes the page control request content

Startreaddatetime start receiving time

The ID list of the file upload control on the fileconids page. When the File Upload control is running on the server, it is the uniqueid of the control.

Guid: the unique identifier of the uploaded file.

Abort: Set and read whether the upload process is interrupted

Ratio upload speed, returns the number of bytes uploaded per second

Formatratio obtains the formatting upload speed, which is expressed in the appropriate bytes, K bytes, and M bytes.

Lefttime estimates the remaining upload time, in seconds

Formatlefttime: Get the remaining upload time in the format, which is represented by hours, minutes, and seconds.

Currentfile: The file being processed.

Status upload status

Formatstatus returns the status of the current upload operation in string format

SaveFile stores the uploaded data files in another way. This method mainly moves the files from the temporary storage directory to the directory required by the user. This method is called because the file is moved.

After the temporary file is successfully deleted, the file corresponding to the temporary file will be removed.

Getfilenamebycontrol: obtains the uploaded file name based on the page file upload Control name.

Dispose all files received in the current context

Getfilename: obtains the uploaded file based on the file upload Control name.

5.5 uploadcontextfactory
Create and obtain a file upload environment.

Inituploadcontext (system. Web. UI. Page, string tempfiledir) obtains a file upload object based on the page object and the temporary directory folder for uploading files.

Context instance

Getuploadcontext obtains the File Upload context based on the upload session number sent by the page.

Getuploadcontext (string guid) obtains the File Upload context based on the guid of the session number.

After page logic processing is complete, release releases the upload context and deletes temporary files.

Uploadstatus: File Upload Status enumeration class

6 usage and precautions
1. Copy the component DLL to the bin directory of the application system, and then reference this component in vs. net,

2. added the ASP. Net Processing Module in Web. config.

<Httpmodules>

<Add name = "bigfileuploadmodulehandle" type = "helpsoft. bigfileuploadmodulehandle, bigfileuploadhandle"/>

</Httpmodules>

3. Mount the event or initialization event on the page of the Asp.net page of the file to be uploaded, initialize the File Upload environment, and register the Temporary Folder for file upload and storage, as shown below:

Surface code:

Private void page_load (Object sender, system. eventargs E)

{

Uploadcontext context = uploadcontextfactory. inituploadcontext (this, @ "C:" myupload "");

}

Tip: A. Set the access permission for the file storage directory. The directory must exist.

B. After the upload environment is initialized, the page must send files within one day. Otherwise, the upload environment becomes invalid.

C. When the upload environment is initialized, the component registers hidden domains on the page to record the unique identifier of the upload session. The hidden field is named uploadid, and the page cannot have controls with the same name.

D. The encoding method of the file form to be uploaded must be multipart/form-data (the system automatically adds

This encoding method, otherwise you need to manually add)

E. To make this Upload Component take effect, you must initialize the upload environment and set the form encoding mode when accessing the page. Otherwise, if you do not want this component to generate

Only one of the first two conditions can be canceled.

4. In the file upload and submission event, you can directly obtain the File Upload environment from the factory class of the file upload environment to further obtain the uploaded file information. The Code is as follows:

Uploadcontext context = uploadcontextfactory. getuploadcontext ();

String filename = context. getfilenamebycontrol (resfile. uniqueid );

String filepath = @ "C:" workdir ";

If (context. SaveFile (resfile. uniqueid, filepath + filename ))

{

Uploadcontextfactory. Release ();

}

Tip: A. When obtaining the corresponding Upload File Information Based on the Control name, you must pass in the unique control identifier for the server control. For non-Server Control, you must enter the control name.

B. Access to directories when files are stored in another directory, whether the directory exists, and the size of available space, which should be checked and processed by the page caller. If this type error occurs, the system will

Exception.

C. If the file corresponding to the control is not found (it may be because the client does not specify a file or the control flag is incorrect), the corresponding file in the temporary directory does not exist (this file has been successfully called

), This method returns false. After the operation is successful, the method returns true.

D. uploadcontextfactory. Release () will clear the upload environment object from the system cache. The getuploadcontext method cannot be used to obtain the upload environment,

The upload environment is still available.

5. When reading the File Upload progress, you need to create another ASPX page. The reading progress is mainly based on the unique identifier of the file upload session

Getuploadcontext (string guid) method) to obtain the Upload File environment. You can access this environment class to obtain the File Upload start time, total information length

Transfer Information length, speed, estimated remaining time, and other information. The system then uses the timed refresh method to continuously obtain the upload progress information to form the upload progress prompt.

You can use XML to display progress without refresh, or you can use regular page refresh.

6. to interrupt the transfer process during transmission, you only need to use the unique session flag to obtain the upload environment and set the abort attribute of the upload environment to true.

The user is disconnected and the upload process is interrupted. Other data submitted on the page is also canceled.

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.