No component Upload progress bar solution

Source: Internet
Author: User

Asp no-component Upload progress bar solution

Asp no component Upload progress bar solution (original address: http://webuc.net/dotey/archive/2004/07/22/1334.aspx)
I. Principle of component-less Upload
Let me describe it with an instance at 1.1. The client HTML is as follows. To browse and upload attachments, we use the <input type = "file"> element, but be sure to set the enctype attribute of form to "multipart/form-data ":


<Form method = "post" action = "upload. asp" enctype = "multipart/form-data">
<Label>
<Input type = "file" name = "file1"/>
</Label>
<Br/>
<Input type = "text" name = "filename" value = "default filename"/>
<Br/>
<Input type = "submit" value = "Submit"/>
<Input type = "reset" value = "Reset"/>
</Form>


In the background asp program, it is very easy to obtain the ASCII data submitted by the form in the past. However, to obtain the uploaded file, you must use the BinaryRead method of the Request object. The BinaryRead method reads the specified number of bytes of the current input stream in binary format. It is worth noting that once the BinaryRead method is used, the Request. Form or Request. QueryString set cannot be used any more. Combined with the TotalBytes attribute of the Request object, all the data submitted by the form can be converted to binary, but the data is encoded. First, let's take a look at how the data is encoded, and whether there are any rules to follow. In the code, we convert binary data read by BinaryRead into text and output it, upload. asp (note that this example does not upload large files, otherwise the browser may die ):
<%
Dim biData, PostData
Size = Request. TotalBytes
BiData = Request. BinaryRead (Size)
PostData = BinaryToString (biData, Size)
Response. Write "<pre>" & PostData & "</pre>" 'uses pre, which is in the original output format.
'Use RecordSet to convert binary data into text
Function BinaryToString (biData, Size)
Const adLongVarChar = 201
Set RS = CreateObject ("ADODB. Recordset ")
RS. Fields. Append "mBinary", adLongVarChar, Size
RS. Open
RS. AddNew
RS ("mBinary"). AppendChunk (biData)
RS. Update
BinaryToString = RS ("mBinary"). Value
RS. Close
End Function
%>


For simplicity, upload the simplest text file (G: homepage.txt, content is "Baoyu: http://www.webuc.net") to test, text box filename to retain the default value "default filename ", submit and view the output result:

----------------------------- 7d429871607fe
Content-Disposition: form-data; name = "file1"; filename = "G: homepage.txt"
Content-Type: text/plain
Baoyu: http://www.webuc.net
----------------------------- 7d429871607fe
Content-Disposition: form-data; name = "filename"
Default filename
----------------------------- 7d429871607fe --

It can be seen that for the items in the form, the boundary "------------------------- 7d429871607fe" is used to separate them into one piece. Each piece has some description information at the beginning, for example: content-Disposition: form-data; name = "filename". In the description information, the name of the form item can be known through name = "filename. If there is Content such as filename = "G: homepage.txt", it indicates that it is an uploaded file. If it is an uploaded file, the description will contain a line of Content-Type: text/plain to describe the Content-Type of the file. Description information and subject information are separated by line breaks.

Well, it's basically clear. Based on this rule, we know how to separate the data and then process the separated data, but we almost ignored a problem, is the boundary value ("----------------------------- 7d429871607fe" in the above example) how to know? The boundary value for each upload is different. Fortunately, you can use the Request in asp. serverVariables ("HTTP_CONTENT_TYPE"). For example, the content of HTTP_CONTENT_TYPE in the preceding example is: "multipart/form-data; boundary = ------------------------- 7d429871607fe, we can not only judge whether the client's form uses enctype = "multipart/form-data" (if it is not used, then there is no need to execute it below ), you can also obtain boundary = --------------------------- 7d429871607fe. (Note: the obtained boundary value is less "--" than the preceding boundary value. It is best to add it .)

As for how to analyze the data, I will not go into details. It is nothing more than using functions such as InStr and Mid to separate the data we want.

II. Multipart Upload, recording progress
To reflect the progress bar in real time, we need to know how much data the current server has obtained in real time? Let's look back at the upload process through Request. BinaryRead (Request. TotalBytes). During the Request process, we cannot know how much data the current server has obtained. So we can only use a work ing method. If we can split the obtained data into one piece, then we can calculate the size of the uploaded part based on the number of uploaded parts! That is to say, if I use 1 K for 1 piece, the input stream of 1 MB will be divided into 1024 pieces for acquisition. For example, if I have obtained 100 pieces, it indicates that 100 K has been uploaded. When I proposed the multipart method, many people thought it was incredible because they ignored the BinaryRead method to read not only the specified size, but also the specified size.

Write an example to verify the integrity of multipart read.

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.