Uploading files in ASP and VBScript (i)

Source: Internet
Author: User
Tags format contains file upload functions header key variable access
Vbscript|vbscript uploading files from a browser is an easy way to deliver files from a client to the server. From the third-generation browser Netscape and Microsoft, most browsers can upload files to the server without having to provide special access or software to the user.

Some ASP components are designed for file uploads, such as:
Posting acceptor
(Part of the Microsoft SiteServer),
Aspsmartupload (Advantys),
AspUpload (Persistssoftware),
Sa-fileupsoftware artisants)

The beginning of this article will tell you about creating such components, which are commonly used in VB, C + +, or Java.

The problem with these components is that they are part of a third-party product rather than a standard ASP. As a third-party component, you must install on the server. This means that you must copy the DLL and register it on the server. Most host systems do not allow such settings on their servers because configuration problems, especially virtual hosts, can occur. The second disadvantage is that most of them are not free, do not provide source code, and can not be customized as needed.

So I need to write VBScript code to solve the file upload problem. This is not an inevitable choice, because VBScript is a scripting language that can only use variants data types and does not provide many built-in functions to manage binary data and byte arrays.

To understand the upload process, you first need to know how the data is sent from the browser to the server using the HTTP protocol. This means that you want to understand form submissions for "Multipart/form-data" (Multi-part/format-data).

Uploading forms

Typically, you use HTML forms to pass data from the browser to the server. This form may contain text fields, check boxes, buttons, and file type control for uploaded files. The user populates and submits the table to the server with his or her own data.

The Enctype property in the form element prescribes the content type encoded by the table dataset that is passed to the server. The default value for the Enctype property is "application/x-www-form-urlencoded", but this default type is not sufficient when a large amount of text is transferred to the server, including non-ASCII characters or binary data. At this point, the file upload submission form should use the "multipart/form-data" content type.

A "multipart/form-data" message contains a series of parts, each of which may contain:
A content-disposition (Content-processing) header with a value of "Form-data" and a name (name) attribute that prescribes the control name.

For a file type control, a part may contain more information:
Specify the filename (filename) attribute of the original path and filename on the client, and the Content-type (Content-type) header of the binary data control being sent.

Follow the control's binary or textual content behind these headers.

The following example illustrates the encoding of "Multipart/form-data", which the client's browser should have:

If this form is submitted, you can read these requests on the server:

-----------------------------7cf87224d2020a
Content-disposition:form-data; Name= "Email"
PhCollignon@email.com
-----------------------------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 sent back to the client as a response, it is displayed. Binary data should be read and written using the Request.BinaryRead and Response.BinaryWrite methods.

〈%
Response.BinaryWrite (Request.BinaryRead (request.totalbytes))
%〉

You can see that the parts of the response are divided by dividing lines:
-----------------------------7cf87224d2020a
The last line followed is '--'.

Every control has a content-disposition. The Name property recognizes the controls sent by the HTML table (email, blob, and enter). For a file type control (BLOB),
The filename is also part of the content-disposition header, and the Content-type header gives the content type of the binary data.

Upload Script

All content must be decomposed. In VB or C + +, this is obvious because many objects and methods are provided for this purpose. In VBScript, you must use some of the functions provided by the language and solve the problem of double-byte encoded variable strings used in VBScript.

VBScript functions

The raw data is in binary format, so you must use a VBScript function designed specifically to manage binary data. Because we consider the raw data as a byte string, the MidB, InstrB, and LenB functions are useful. However, to avoid classic strings in VBScript, because they are double-byte encoded strings, it is not appropriate to break down into single-byte.

These are the only functions in the VBScript function that are used to decompose bytes. A method is also needed to get a double-byte encoded string from the decomposed data so that the strings in VBScript code can be used. To use a string as an argument in InStrB, a function is also needed to convert a double-byte string into a single-byte string.

To write two functions for me, getString () and getbytestring () are explained later.

Structure

The decomposed data is stored in the VBScript Dictionary object. The Dictionary object is a hash table object that stores (key, item) pairs. It's part of VBScript and ASP2.0.

Defines the first Dictionary object "Uploadrequest". This object contains all the controls submitted by the upload table. Key is the name of the control, and item is the control information contained in the object:
"ControlName1", Dictionary control1
"ControlName2", Dictionary control2

The Dictionary object representing a control contains the following (key, item) pairs:
' Value ', String or binary content
' FileName ', Name of uploaded file
"ContentType", ContentType of uploaded file

To combine these, we have the following examples:

Uploadrequest: "Email", Uploadcontrol 1: "Value", PhCollignon@email.com
"Blob", Uploadcontrol 2: "filename", c:/image/file.gif "ContentType":
Image/gif "Value": Gif89ai?
This object is useful for future access and use of data.

Decomposition

Here is the code to decompose, read, and record the upload control. This process is done using the "Builduploadrequest" program, which has only one independent variable, the original binary data requestbin.

Sub Builduploadrequest (Requestbin)

The first step is to find the dividing line, by dividing the line to know when the control loop ends.

' Get the boundary Posbeg = 1 Posend = InstrB (posbeg,requestbin,getbytestring (Chr (13))
Bou



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.