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

Source: Internet
Author: User
Tags server memory
ArticleDirectory
    • 5.1 bigfileuploadmodulehandle
    • 5.2 datareader
    • 5.3 upfileinfo
    • 5.4 uploadcontext
    • 5.5 uploadcontextfactory

1. Writing Purpose

For ease of useUpload a large file component and close up the user manual to specify the interface for calling the component, the operation process and precautions.

2 Background

BecauseASP. NETDuring file uploading, all files are loaded into the server memory. During file uploading, many resources are consumed on the server, and the upload progress cannot be displayed. Because many server resources are consumedASP. NETThe size of an uploaded file is limited. This component solves all these problems

3 Definition

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

Web. config: YesASP. NETConfiguration file.

4 Function

All requiredASP. NETYou can use this component to upload files. This component can be releasedASP. NETLimits the size of the uploaded file and provides the upload progress prompt. You can also choose to interrupt the upload process. Reduces the consumption of server resources (tested, server resource consumption and normal operationASP. NETPage almost)

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.IISSend dataASP. NETIn the MPs queue, read the data, write the file, and re-organize the request content. The re-organized content removes the request content of the file content. After the page receives the request, the attributes of other components can be normally accessed except the File Upload control cannot read the file content.

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 objects on the file upload page and temporary directory for storing files. Some constructors cannot be called externally. To build a file upload environment, you must callUploadcontextfactoryStatic method provided

Tmepfiledir: Get and set the path for storing uploaded temporary files

FilenamesList of uploaded file names

TotallengthThe total length of the message to be sent, including the request content of all controls on the page.

ReadedlengthLength of received information Page control request content

StartreaddatetimeReceiving Start Time

FileconidsPage file upload ControlIDList, which isUniqueid

Guid: Unique Identifier of the uploaded file

Abort : Set and read whether the upload process is interrupted

RatioUpload speed,Returns the number of bytes uploaded per second.

FormatratioGet the formatted upload speed in the appropriate byte,KByte,MByte Representation

LefttimeEstimated remaining upload time,In seconds

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

CurrentfileThe file being processed.

StatusUpload Status

Formatstatus Returns the status of the current upload operation in string format.

SaveFileSave the uploaded data file in another way. This method mainly moves the file from the temporary storage directory to the directory required by the user. Because the file is moved, after this method is called successfully, files corresponding to temporary files will be removed.

GetfilenamebycontrolObtain the uploaded file name based on the page file upload Control name.

DisposeAll files received in the current context

Getfilename : Obtain 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 context class instance based on the page object and the temporary directory folder for uploading files.

GetuploadcontextObtains the File Upload context based on the upload session number sent on the page.

Getuploadcontext (string guid)According to the session IDGuidObtain the File Upload Context

ReleaseAfter the page logic processing is complete, release the upload context and delete the temporary file.

Uploadstatus: File Upload Status enumeration class

6 Usage and precautions

1,Add ComponentsDLLCopy to Application SystemBinDirectory, and thenVs. netReference this component,

2,InWeb. configAddASP. NETProcessing Module

<Httpmodules>

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

</Httpmodules>

3,InAsp.netPage Mount events or initialization events, initialize the File Upload environment, and register the Temporary Folder for file upload and storage, as shown belowCode:

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

{

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

}

Tip:A, File storage directory requires the corresponding access permissions, the directory must exist

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

CWhen the upload environment is initialized, the component registers hidden domains on the page to record the unique identifier of the upload session. Name the hidden domainUploadid,The page cannot have controls with the same name.

DThe encoding method of the file form to be uploaded must beMultipart/form-Data(When the file upload control is run as a server control, the system automatically adds this encoding method. Otherwise, you must manually add the encoding method)

ETo 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 take effect for a specific page, 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:AWhen obtaining the corresponding Upload File Information Based on the Control name, you need to pass in the unique control identifier for the server control. For non-Server Control, you need to pass in the control name.

BThe access permission to the directory where the file is stored, whether the file exists, and the size of available space, which is checked and processed by the page caller. If such a type error occurs, an exception is thrown.

CIf the file corresponding to the control is not found (it may be that the client has not specified a file or the control flag passed in is incorrect), the corresponding file in the temporary directory does not exist (This method has been successfully called, object has been removed). This method returns false. After the operation is successful, the method returns true.

D,Uploadcontextfactory. Release ()The upload environment object will be cleared from the system cache and will not be availableGetuploadcontextMethod to obtain the upload environment, but the obtained upload environment is still available.

5,When reading the File Upload progress, you need to create anotherAspxPage, the read progress is mainly based on the unique identifier of the Session of the uploaded fileGetuploadcontext (string guid)Method) to obtain the Upload File environment, access this environment class, that is, you can obtain the File Upload start time, total length of information, the current length of transmitted information, 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.

Progress display can be usedXMLNo refresh method is available. You can also use the page regularly refresh method.

6,To interrupt the transfer process during transmission, you only need to use the unique session flag to obtain the upload environment.AbortIf the property is set to true, the system will disconnect the client and interrupt the upload process. Other data submitted on the page will also be canceled.

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.