Making files from the browser is a simple way to pass files from clients to the server. From the third -generation browser NetScape and Microsoft, most browsers can upload files from the server without the need to provide users with special access methods or software.
Some ASP components are designed for files, for example::
Posting Acceptor
(Part of Microsoft Siteserver),
Aspsmartupload (advantys),
aspupload (PersistSsoftware),
Sa-Fileupsoftware Artisants)
The beginning of this article will tell you information about creating such components, and these components usually use VB, C ++ or Java.
The problem of these components is that they are part of third -party products rather than standard ASP. As a third -party component, installation must be installed on the server. This means that DLL must be copied and registered on the server. Most hosting systems are not allowed to perform such settings on their servers because configuration problems (especially virtual hosts) may occur. The second disadvantage is that most of them are not free. If they do not provide source code, they cannot be customized as needed.
Therefore, I need to write the VBScript code to solve the problem of uploading the file. This is not an inevitable choice, because VBScript is a script language, which can only use the Variants data type, and cannot provide many built -in functions of managing binary data and byte array.
To understand the process of uploading, we must first know that the data is sent from the browser to the server with the HTTP protocol. This means that the form submission of "Multipart/Form-Data" (multi-part/format-data) is submitted.
Upload form
Under normal circumstances, use the HTML form to pass data from the browser to the server. This form may contain text domains, inspection boxes, buttons, and file type control of uploaded files. The user fills and submits this table to the server with his own data.
The enableype attribute in the table element specifies the content type encoded by the table data set encoded to the server. The default value of the Enctype attribute is "Application/X-WWW-Form-Urlencoded", but when transmitting a large amount of text to the server, including data with non-ASCII characters or binary numbers, this default type cannot be competent. At this time, the "Multipart/Form-Data" content type should be used when the file uploads the form.
A "Multipart/Form-Data" information contains a series of components. Each component may include:
A Content-Disposition head, the value of "Form-Data"; a name (name) attribute that specifies the control name.
For a file type control, one part may contain more information:
Filename (file name) attributes of the original path and file name are stipulated on the client; the connected data controlled by binary data control (content-type) header.
Follow the control binary or text content behind these heads.
The following example illustrates the encoding of "Multipart/Form-Data". The browser of the client should have this form:
If this form is submitted, you can read these requests on the server:
-----------------------------7cf87224d2020a
Content-Disposition: Form-Data; name = "Email"
[Email Protected]
-----------------------------7cf87224d2020a
Content-Disposition: Form-Data; name = "blob"; filename = "c: /Image.gif"
Content-Type: Image/PJPEG
-----------------------------7cf87224d2020a
Content-Disposition: Form-Data; name = "Enter"
submit query
-----------------------------7cf87224d2020a--
When that content is transmitted as a response to the client, it will be displayed. Request.binaryread and response.binarywrite method read and write binary data.
< / Percent
Response.binarywrite (request.binaryread (request.totalbytes))
%>
You can see the division of each part of the response:
-----------------------------7cf87224d2020a
The last boundary line follows ' -'.
Each control has a Content-Disposition. The name attribute recognition is controlled by the HTML table (Email, Blob, and Enter. For a file type control (BLOB),
The file name is also part of the head-disposition header, and the content-type header gives the content type of binary data.
Uploaded script
All the above contents must be linked. This is very obvious in VB or C ++ because there are many objects and methods for this. In VBScript, some functions provided by the language must be used, and the problem of dual -byte encoded variable string used in VBScript must be solved.
VBScript function
The original data is a binary format, so the VBScript function designed for managing binary data must be used. Because we consider the original data as a strings of bytes, MIDB, Instrb, and Lenb functions are useful. But avoid VBSCRIPT's classic string, because they are dual -byte -encoded string, which is not suitable to decompose into single bytes.
These are the only functions used to decompose bytes in the VBScript function. A method is also required to obtain a dual -byte encoded string from the decomposed data so that you can use the string in the vbscript encoding. In order to use the string as an independent variable in Instrb, a function is needed to convert the dual -byte string into a single -byte string.
For me, I wrote two functions, getstring () and getBytestring (), and then explained it later.
structure
The decomposed data is stored in the VBScript Dictionary object. The Dictionary object is the HASH table object, which stores (key, item). It is part of VBScript and ASP2.0.
Define the first Dictionary object "uploadRequest". This object contains all controls submitted by the uploading table. Key is the control of the control, and item is the control information contained in the object:
"ControlName1", Dictionary Control1
"ControlName2", Dictionary Control2
Representing a controlled Dictionary object contains the following (key, item) pair:
"Value", string or binary Content
"Filename", name of uploaded file
"ContentType", ContentType of UPloaded File
Combining these, there are the following examples:
uploadRequest: "Email", uploadControl 1: "Value", [Email Protected]
"blob", uploadControl 2: "Filename", C: /upload/200902/file.gif "ContentType":
Image/GIF "Value": GIF89ai?
This object is very useful for future access and use data.
break down
Here is the code for decomposition, reading and record uploading control. This process is completed with the "BuilduploadRequest" program. This program has only one independent variable, which is the original binary data Requestbin.
sub builduploadRequest (requestbin)
First of all, you must find the dividing line. You can know when the control cycle is over through the dividing line.
'get the boundary posbeg = 1 posend = Instrb
bou
|||