Uploaded script use
Below is an example of the uploaded script for development. The download file in this article provides the files and code in the example. Release the compressed file to a path and configure a virtual path for your network server. You can test and start the uploadForm.html in the browser.
Call script
Below is the way to call up to builduploadRequest method. First call a full process: uploadRequest. Then call the BUILUPLOADREQUEST method, and then transmit it to the request original binary data in the independent variable.
bytecount = request.totalbytes
Requestbin = Request.binaryRead (bytecount)
dim uploadRequest
set uploadRequest = CreateObject
BuilduploadRequest Requestbin
The data is decomposed and stored in the Dictionary object and recovered with the Item () method. These itEM data can be stored in VBSCRIPT variables and can be used anywhere in the code. Data can be transmitted as a response to the client, or used in ASP code, or in the file and put in the database.
Retrieve data
The data of the UPLOADREQUEST object can be accessible with the Item ("key") function. Let's consider this situation now: to access the value of an email control. You can do this:
email = uploadRequest.item ("email"). Item ("value")
Because this is a text control, the content is a string, this string can be used like any other VBScript string. For binary data, you can restore the content in the same method:
Piction = uploadRequest.Item ("blob"). Item ("Value")
You can also access other information, such as file name and Content-Type. They are text control.
ContentType = UploadRequest.Item ("Blob"). Item ("ContentType")
filepathName = UploadRequest.Item ("Blob"). Item ("FILENAME")
Use data in VBScript code
The uploaded data can be used in the VBScript code like other variables. For example, they can send back to the client as a response.
Your Email is: < % = Email %>
File name of young is < % = filepathName %>
File Type of Your Piction is < % = ContentType %>
Binary data can also be sent back to the client. A Content-Type must be set, and binary data can be used to write the binarywrite method.
response.contenttype = ContentType Response.binaryWrite Piction
Write uploaded data to the file
In the case of file class control, the purpose is to store binary data into a file or database domain instead of transmitting them back to the client. This purpose is the inherent characteristics of uploading files. Use the FileSystem object to store the uploaded file into the file system of the server.
First create a FileSystem object:
'Create FileSytemobject Component SCRIPTObject = Server.createObject ("Scripting.fileSystemObject")
Use the FileSystem object to create a file in the path. The path can be absolute, directly pointing to the file system (such as C:/Temp). It can also be relative, under a virtual path defined by the network server. Use the MAPPATH method and the Path_info server variable to take the virtual path to the absolute path.
The WRITE method requires a dual -byte string as an independent variable, so the single byte number is converted into a string. The WRITE method is responsible for converting this dual -byte string and writing it in ASCII format. This establishes a binary content containing our original single -byte string. I have named this file "Uploaded + FILENAME". This is just to distinguish the file. You can use any other file name, 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
Start up the uploaded data into the database
Data can also be stored in the database. Content-Type should also be stored in the database so that data can be displayed later. First of all, we must establish a connection with the database.
set conn = server.createObject ("adodb.connection")
conn.open "dsn = Wroxdns", "user", "pass"
Then create a record set from the connection:
SQL = "Select Photo, ContentType from Mytable"
set rs = server.createObject
RS.Oopen SQL, CONN, 3, 3, 3
After the record set is created, put binary data into the BLOB domain of the database:
pictureChunk = Picture & chrb (0)
RS.Fields ("Piction").
RS.Fields ("ContentType") = ContentType
rs.update
conn.close
In the APENDCHUNK method, I have to solve a bug. In fact, I noticed that when binary data have strange number bytes, the Appendchunk method does not transmit the last byte. The solution is to add a CHR (0) to ensure that all bytes are transmitted. Maybe there are other methods, if so, please tell me.
To get the reflection of the database, use the same record set, and use the correct content type to send it back to the client as a response.
response.contenttype = RS.Fields ("ContentType")
size = RS.Fields ("Piction"). ActualSize
blob = RS.Fields ("Piction"). Getchunk (size)
response.binarywrite blob
in conclusion
This article shows a complete way to file the file with VBSCRIPT. The encoding is completely VBScript, independent of third -party products.
First introduce the uploading process (using the "Multipart/Form-Data" content for HTML). Then introduce the uploaded VBScript code in detail. At the beginning, a brief review of the VBScript function of the operating string and a single byte number series. Then introduce the code of the script and the structure of uploading the data.
Finally, multiple purposes of this script, from uploading variables in ASP code to database or file systems to store upload files.
Click the link below to download <a href = "http://www.asptoday.com/articleS/images/20000316.zip"> The routine code of this article.