ASP no component upload progress bar solution

Source: Internet
Author: User
Tags reset

The principle of no component upload

I am still 1.1 points with an example to illustrate the bar, the client HTML is as follows. To browse the upload attachment, we pass <input type= "file" > element, but be sure to note that the Enctype property of the form must be set 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 get the ASCII data submitted by the form before. However, if you need to obtain the uploaded file, you must use the BinaryRead method of the Request object to read it. The BinaryRead method is binary read of the specified byte number for the current input stream, and it is somewhat noteworthy that once the BinaryRead method is used, the Request.Form or Request.QueryString collection can no longer be used. Combined with the TotalBytes property of the Request object, all the form submissions can be turned into binary, but the data is encoded. Let's start by looking at how the data is encoded, there is no law to follow, coding code, in the code we will binaryread read the binary into text, output out, in the background of the upload.asp (note that the example does not upload large files, or may cause the browser to die):

<%
Dim biData, PostData
Size = Request.TotalBytes
biData = Request.BinaryRead(Size)
PostData = BinaryToString(biData,Size)
Response.Write "<pre>" & PostData & "</pre>" '使用pre,原样输出格式
' 借助RecordSet将二进制流转化成文本
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 one of the simplest text files (G:\homepage.txt, "Gem: Http://www.webuc.net") to test, the text box filename retains the default value "file default filename", Submit to see output results:

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

As you can see, for the items in the form, they are separated into a piece with a boundary such as "-----------------------------7d429871607fe", and each piece has some descriptive information at the beginning, For example: Content-disposition:form-data; Name= "FileName", in the description information, through name= "filename" can know the name of the table item. If there is a filename= "G:\homepage.txt" Such content, the description is an uploaded file, if it is an uploaded file, then the description information will be more than one line content-type:text/plain to describe the file content-type. The description information and the principal information are separated by a newline.

Basically clear, according to this law we know how to separate data, and then the separation of data processing, but almost ignore a problem is the boundary value (in the example of the "-----------------------------7d429871607fe") How did you know that? Each upload this boundary value is not the same, fortunately fortunately, the ASP can be Request.ServerVariables ("Http_content_type") to obtain, such as in the above example Http_content_type content is: " Multipart/form-data; boundary=---------------------------7d429871607fe ", with this, 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, and you can get the boundary value boundary=---------------------------7d429871607fe. (Note: The boundary value obtained here is less than the threshold of the above boundary value "--", preferably added.) )

As for how to analyze the process of data I do not repeat, nothing more than the use of such functions such as instr,mid to separate out the data we want.

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.