ASP. NET large File Upload Component Development Summary (2)-extraction of File Content

Source: Internet
Author: User

I don't know when the earthquake will end. In order to watch the night for my wife and children, it seems that I can't go to bed. I just wrote the second article, but I don't know if it is useful to everyone.

To provide the file content, we need to first determine the file content sent in the customer request, and then determine the location of the file content. This part correspondsCodeAs follows:

1 Httpapplication app = Sender As Httpapplication;
2 Httpworkerrequest request = Getworkerrequest (App. context );
3
4 If ( ! Isuploadrequest (App. Request )) Return ; // If the file is not uploaded, exit.
5
6 String Scontenttype = App. Request. contenttype. tolower ();
7 Byte [] Arrboundary = Getmultipartboundary (scontenttype );
8 Int Contentlength = App. Request. contentlength; // Total length of information body
9
10 Datareader =   New Datareader (App. Context. Request. contentencoding, arrboundary );
11 Datetime startdate = Datetime. now;
12 Byte [] Arrbuffer = Request. getpreloadedentitybody ();
13 If (Arrbuffer =   Null )
14 {
15 Arrbuffer =   New Byte [ 0 ];
16 Tempfile. Close ();
17 Return ; // No information body is read
18 }
19 Else
20 {
21Here is the processing of the file content
22}

In the above Code, we first obtain ASP. net to the client request processing object httpworkerrequest, and then determine whether the corresponding request has uploaded files based on whether the contenttype attribute of this object is multipart/form-data. If no file is uploaded, this request is not processed to improve processing efficiency. In this way, the corresponding enctype attribute is multipart/form-data in the HTML form uploaded with a file. This solves the problem of determining whether the client request sends any file content.
To determine the file content location, we need to use the reflector tool for decompilation. System. Web. dll Medium Httprequest You can find that there is a getentirerawcontent method. In this method, the getpreloadedentitybody method of httpworkerrequest is called to obtain data. Fortunately, this method is public, therefore, you can call this method to obtain the data sent by the client.
After obtaining the data, we can write the data into a file, which is of the byte [] type. When writing a file, because HTTP is text-based, so we can use system. text. encoding. the getstring method encodes these byte arrays into strings. Here I use ASCII encoding, but the Chinese characters in the request become garbled characters. In this way, you can obtain the style and format of the request content for analysis. (In the code, I have removed the file writing function, because all the request content is only used for analysis, see Article 1 Article Examples of the request content listed in ).
By analyzing these request content, we can find that each page control can find the corresponding content here. The two controls are separated by the string "------------------------------- 7d81e441d025c. At the same time, in our Decompilation System. Web. dll Medium Httprequest You can also use the getmultipartboundary method in the Code. This method can be understood literally as a separator. We extract this method: 1 Private   Byte [] Getmultipartboundary ()
2 {
3 String Attributefromheader = Getattributefromheader ( This . Contenttype, " Boundary " );
4 If (Attributefromheader =   Null )
5 {
6Return Null;
7}
8 Attributefromheader =   " -- "   + Attributefromheader;
9 Return Encoding. ASCII. getbytes (attributefromheader. tochararray ());
10 }

Then, we can extract strings like "------------------------------------- 7d81e441d025c" from the request content. In this way, we can separate the content of different controls on the page.
Finally, we analyze the content of the file upload control:

Content - Disposition: Form - Data; Name = " File1 " ; Filename = " C: \ Documents ents and Settings \ Administrator \?????? \ Componentart.web.ui.rar "
Content - Type: Application / X - RAR - Compressed

RAR! ??S

We can find that the file upload control has a filename attribute. Then, specify the MIME type of the file in the next row. Then, this blank line is displayed under the blank line, until the next mark string is similar to "------------------------- 7d81e441d025c", it is the file content sent by the client. We only need to write the content to the Server File.

Here, the problem of file content extraction can be solved. When extracting the file content, special processing is required for processing the flag string to prevent the flag characters from being read to the two buffers respectively. Therefore, this part of code is still relatively large. For details, seeSource codeDatareader class.

I wrote this article in the second article. It's too difficult to write it 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.