ASP. NET large File Upload Component Development Summary (3)-processing File Content

Source: Internet
Author: User
This article is relatively simpler. You only need to determine the file processing ideas and the issue of transferring ASP. NET page information.

. In net1.1, Asp. net puts the obtained request content into a byte [] type, but in ASP. in net2.0, data is encapsulated into a type of system. web. in the httprawuploadedcontent object, there is an important attribute in this new object: _ file. The type of this attribute is tempfile. This type provides the ability to save the request content to a temporary file. This information may be obtained by decompiling the reflector tool (so this tool is essential for. NET development ).

In the httprequest class, we can find the following: Code : Private Multipartcontentelement [] getmultipartcontent ()
{
If ( This . _ Multipartcontentelements =   Null )
{
Byte [] Multipartboundary =   This . Getmultipartboundary ();
If (Multipartboundary =   Null )
{
Return NewMultipartcontentelement [0];
}
Httprawuploadedcontent entirerawcontent =   This . Getentirerawcontent ();
If (Entirerawcontent =   Null )
{
Return NewMultipartcontentelement [0];
}
This . _ Multipartcontentelements = Httpmultipartcontenttemplateparser. parse (entirerawcontent, entirerawcontent. length, multipartboundary, This . Contentencoding );
}
Return   This . _ Multipartcontentelements;
}

Having said so much, it just illustrates a problem: it is troublesome to transfer data to pages by changing the attribute values of ASP. NET page objects. Therefore, I adopt a single-piece design approach. Specifically, the obtained file content is written to the disk file. During each file upload process, a session flag is initialized, and the page uses this session flag to extract the file information. In the system, two classes are defined to handle this task:
The uploadcontext class is used to encapsulate file information.
Class uploadcontextfactory is used for session flag management.

On the upload page, the component registers a hidden field named uploadid on the page. Store a guid as the session ID. This flag appears in the request content during file upload. The component correctly saves the file to the temporary directory by obtaining the session flag and file content, and extract the file information for the page. Based on the request ["uploadid"] field, the uploadcontextfactory class extracts the session flag corresponding to the uploaded file and obtains the file information. In this way, the page is extracted from the file content.

In this way, you must use the following statement in the page_load event of the upload page to initialize a session and specify the directory where the file is stored.

Uploadcontext Context = Uploadcontextfactory. inituploadcontext ( This , @" C: \ myupload \ " );

Where file information needs to be obtained: if the statement is used for obtaining: Uploadcontext Context = Uploadcontextfactory. getuploadcontext ();
If (Context ! =   Null ) && (Context. Status = Uploadstatus. Complete ))
{
Context. SaveFile (file1.clientid, request. mappath ("/Myupload/"));
}


Of course, there is a problem here: the session flag expires and the process of uploading files without the initialization flag (such as directly sending files without requesting the page, this may occur when you use other tools ). This issue has not been resolved yet.

Now, the uploaded file can be uploaded to the server, and the content of the uploaded file can be obtained on the page for further processing, such as limiting the file type and storing it in the database.

I feel a little confused, so I will write it out first, and I will sort it out later.

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.