Uploading files in ASP and VBScript (ii)

Source: Internet
Author: User
Tags chr dsn file system client
Use of Vbscript|vbscript upload scripts

The following is an example of an application of the developed upload script. The download file in this article provides examples of files and code. Release the compressed file to a path and configure a virtual path for your network server. You can test and start uploadform.html in the browser.

Calling script

The following is a way to invoke the upload Builduploadrequest method. First call a full dictionary:uploadrequest. The Builuploadrequest method is then invoked and then transferred to the request raw binary data in the argument.

ByteCount = Request.TotalBytes
Requestbin = Request.BinaryRead (ByteCount)
Dim Uploadrequest
Set uploadrequest = CreateObject ("Scripting.Dictionary")
Builduploadrequest Requestbin

The data is decomposed and stored in the Dictionary object, and is recovered using the item () method. These item data can be saved in a VBScript variable and can be used anywhere in the code. Data can be sent back to the client as a response, either in ASP code, or in a file and into a database.

Retrieving data

The Uploadrequest object's data can be accessed using the item ("Key") function. Now consider the situation in which you want to access the value of an email control. You can do this:

email = uploadrequest.item ("email"). Item ("Value")

Because this is a text class control, the content is a string that can be used like any other VBScript string. For binary data, you can recover content in the same way:

Picture = Uploadrequest.item ("blob"). Item ("Value")

You can also access other information, such as file names and Content-type. They are text-class-controlled.

ContentType = Uploadrequest.item ("blob"). Item ("ContentType")
Filepathname = Uploadrequest.item ("blob"). Item ("FileName")

Working with data in VBScript code

The uploaded data can be used in VBScript code like other variables. For example, they can be sent back to the client as a response.

Your Email is: 〈%=email%〉
File name of your picture is〈%=filepathname%〉
File type of your picture is〈%=contenttype%〉

Binary data can also be sent back to the client. You must set up a content-type to write binary data in the BinaryWrite method.

Response.ContentType = ContentType Response.BinaryWrite picture

Write upload data to a file

In the case of File class control, the goal is usually to deposit binary data into a file or database domain, rather than sending them back to the client. This purpose is to upload the inherent characteristics of the file. Use the FileSystem object to save the uploaded file to the server's file system.

First create the FileSystem object:

' Create filesytemobject Component Set scriptobject = Server.CreateObject ("Scripting.FileSystemObject")

Creates a file in the path with the FileSystem object. The path can be absolute, pointing directly to the file system (such as C:\Temp). It can also be relative, to a virtual path defined by the network server. The virtual path is mapped to an absolute path using the MapPath method and the PATH_INFO server variable.

The Write method requires a two-byte string as the argument, so a single-byte sequence is converted to a string. The Write method is responsible for converting this double-byte string and writing it in ASCII format. This creates a file containing the binary content of our original Single-byte string. I have named this file "Uploaded+filename", which is just to distinguish files, you can use any other filename, such as:

' Create and Write to a ' File Set MyFile = Scriptobject.createtextfile (Server.MapPath (Request.ServerVariables _ ("Path_info ) & "uploaded" & filename)
For i = 1 to LenB (value)
Myfile.write chr (AscB (MidB (value, I, 1))
Next
Myfile.close

Storing uploaded data in a database

Data can also be stored in the database. Content-type should also be stored in the database for later display of data. The first step is to establish a connection to the database, assuming that the appropriate DSN has been set:

Set conn = Server.CreateObject ("ADODB. Connection ")
Conn.Open "Dsn=wroxdns", "User", "pass"
Then create a recordset from the connection:

sql = "Select PHOTO, CONTENTTYPE from MYTABLE"
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Rs. Open SQL, Conn, 3, 3
After the recordset is created, put the binary data into a BLOB field in the database:

Picturechunk = picture & ChrB (0)
Rs. Fields ("Picture"). AppendChunk Picturechunk
Rs. Fields ("CONTENTTYPE") = CONTENTTYPE
Rs. Update
Conn.close

In the AppendChunk method, I had to fix a bug. In fact, I noticed that the AppendChunk method does not transmit the last byte when binary data has a singular number section. The solution is to add a Chr (0) to ensure that all bytes are transmitted. There may be other ways, if any, please let me know.

To get the image of the database, use the same recordset and send it back to the client as a response with the correct content type.

Response.ContentType = Rs. Fields ("CONTENTTYPE")
size = Rs. Fields ("Picture"). ActualSize
Blob = Rs. Fields ("Picture"). GetChunk (size)
Response.BinaryWrite blob

Conclusion

This article presents a complete method for uploading files in VBScript. The code is completely VBScript, independent of Third-party products.

The process of uploading is described first (HTML delivery with "Multipart/form-data" content). Then the uploaded VBScript code is described in detail. A brief review of the action string and the VBScript function of a single-byte sequence is performed at the beginning. It then describes the script's code and the structure of the uploaded data.

Finally, a number of uses for this script are shown, from uploading variables in ASP code to storing uploaded files in the database or file system.

Click on the link below to download the <a href= "Http://www.asptoday.com/articles/images/20000316.zip" > This article's routine code.



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.