ASP no component upload • From the principle of analysis to practice (on)

Source: Internet
Author: User
Tags functions split
Upload | No component and no component upload has been a problem that bothers us all. In fact, the principle is very simple, the core is the analysis of strings. However, the actual operation, but the difficulties. One of the key issues is that we often do not delve into the principle, or because the process is too cumbersome, resulting in bugs constantly. All along, want to do a perfect example, just think about headaches, plus no time (excuses, hehe), so did not put into action.

Bite the teeth today and give you an example of a complete component-free upload. Because of my patience is not good, so we come at 1.1, a few days to complete. In the next few days, I will update this document every day, this process is also the process of learning and improving.

(Complete source code and example, can be found here: http://www.2yup.com/asp/attach/A0000006.zip)

==============================================================
The first day: Understanding our anatomy--data

When uploading files, you first need to know what we are getting. Here's a list of uploaded files, and we'll start with him.
<form action= "doupload.asp" Method=post enctype= "Multipart/form-data" >
File1 Description: <input type=text name=file1_desc>
File1<input Type=file name=file1><br>
File2 Description: <input type=text name=file2_desc>
File2<input Type=file name=file2><br>
<input Type=submit name=upload value=upload>
</form>

The meaning of enctype= "Multipart/form-data" in the form is to set the MIME encoding of the form. By default, this encoding format is application/x-www-form-urlencoded and cannot be used for file uploads, and only multipart/form-data can be used to complete the transfer of file data for the following actions (interested friends, You can try the similarities and differences of the two. The method is very simple, is to remove this sentence. Now, we'll fill in the data in the form separately:
File1 's description d:\ my picture \back046. Gif
File2 's description d:\ my picture \back293. Gif

Here the Chinese and English, the space mixed row. The aim is to make examples more general. The two pictures I chose were 54 and 62 bytes. The principle of big picture is exactly the same, but the small picture makes the example more suitable, the principle is easy to show.
In order to see the data we get, in doupload.asp, we have these lines of code:
<%
Formsize=request.totalbytes
Formdata=request.binaryread (Formsize)
Response. BinaryWrite (Formdata)
%>

Very simple, the function is to call out all the data. If you're unfamiliar, you can look at both the request and the response object's methods.

Submit the form, we look in IE inside the HTML source, get:
-----------------------------7d22131090458
Content-disposition:form-data; Name= "File1_desc"

File1μ?? Μ?÷
-----------------------------7d22131090458
Content-disposition:form-data; Name= "File1"; Filename= "D:\?òμ?" Í??? \back046. GIF "
Content-type:image/gif

gif89a ' Ì?f?f3?ì??? Ì!ù,@? Á?o;

-----------------------------7d22131090458
Content-disposition:form-data; Name= "File2_desc"

File2μ?? Μ?÷
-----------------------------7d22131090458
Content-disposition:form-data; Name= "File2"; Filename= "D:\?òμ?" Í??? \back293. GIF "
Content-type:image/gif

GIF89A ('??? Yyyììì!ù, (@l€?j (·) J? N (34ˉ;
-----------------------------7d22131090458
Content-disposition:form-data; Name= "Upload"

Upload
-----------------------------7d22131090458--

No doubt, this is what you conveys from the last "simple" table. Now think about how to deal with this pile of things? Does it look like there's a pattern, and you don't know where to begin? Tomorrow, let's analyze the pile of "pictures" and see how we can isolate what we want.


==============================================================
The second day: a preliminary split

Have a good night's sleep, people are more sober? Lunch today to eat hotpot, Arsenal vs. Iron Brother did not finish, now a brain lard ...
OK, let's go on with this boring problem. First, find the rules. It seems so easy to use
-----------------------------7d22131090458
Do separate, so that every text unit, is
Content-disposition:form-data; Name= "The name of the form field";

The contents of a form field

And in every file unit, it's
Content-disposition:form-data; Name= "The name of the form field"; Filename= "File full path"
Content-type: File type

Binary content of a file

So, is it directly used
Split (Formdata, "-----------------------------7d22131090458")
Can we get the units? The answer is in the negative. First of all, Formdata is not a string but binary string, can not be split method, and secondly, the 7d22131090458 here is not fixed, each time there will be changes, is not suitable for separators. So, you should use a more insurance approach. Do you have any idea? It's simple--use the first line of Formdata as a separator. Just use the INSTRB function to get the position of the line break, and then use the LeftB or MidB function to intercept the data. Let's try this:
<%
' Binary carriage return <return>
BNCRLF=CHRB (+) & ChrB (10)

' Get Formdata
Formsize=request.totalbytes
Formdata=request.binaryread (Formsize)

' Get the Separator
Divider=leftb (FORMDATA,CLNG (INSTRB (FORMDATA,BNCRLF))-1)

' Look, right?
Response. BinaryWrite (Divider)
%>

Run... It worked! Got the divider needed. Note that the string functions here are manipulated for binary data, so they use their binary sedan, adding "B" (binary's first letter)--instrb,leftb (Rightb,midb,lenb may also appear later.) etc.). After all, Formdata is the "BinaryRead ()". OK, some separator, you can get the data. Let's start with a simple one, take a look at the first unit, and the goal is to get the form field name and data.
<%
' This is a carriage return <return>
BNCRLF=CHRB (+) & ChrB (10)

' Get the data
Formsize=request.totalbytes
Formdata=request.binaryread (Formsize)

' Get divider, separator
Divider=leftb (FORMDATA,CLNG (INSTRB (FORMDATA,BNCRLF))-1)

' Start position
startpos = INSTRB (formdata,divider) +lenb (divider) +lenb (BNCRLF)
' Terminate position, starting from starting position to next divider
Endpos = INSTRB (startpos, Formdata, divider)-lenb (BNCRLF)
Part1 = MidB (Formdata, startpos, Endpos-startpos)
Response. BinaryWrite (part1)
%>

This paragraph has a comment, I believe everyone is fine. If you are not aware of these functions, you can download the MSDN reference to http://www.2yup.com/asp/referrence/index.asp to see the functional usage of VBScript, which is a great help in improving your level.
The resulting results can be seen in the way that the generated HTML source is viewed:
Content-disposition:form-data; Name= "File1_desc"

Description of File1



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.