File Upload using a VBScript Class






4.91/5 (43 votes)
May 30, 2002
2 min read

406624

9002
Using a VBScript class to upload files. Useful when you are not able to deploy a custom COM object.
Introduction
There are several components on the market that allow your ASP application to handle file uploads. But when you are in a situation where in you are not allowed to install any components then this vbsUpload will be helpful to you. During my Academic Project I was not allowd to use the components on the server and hence found an alternative to Upload the Files from the client end.
Code Support
- vbsUpload.asp
- This is the file which contains the whole logic of the File Upload. The Request.Binary read method is used to read the raw data sent by the client as part of a POST request. ASP provides a method to apply URL encoding rules, but no publicly exposed way to decode. The URLDecode method does that URLDecoding. Then the raw data is parsed and the File elements and Form Elements are separately kept in two collection objects.
- Process_File.asp
- This is a simple asp file used to Upload the multipart/form-data. Once the data is available this file will save the file on the hard disk. The file is as follows:
<%@ Language="VBScript" %> <!-- #include file="vbsUpload.asp" --> <form method=post enctype="multipart/form-data" action=<%=request.servervariables("script_name")%>> Your File:<BR><input type=file name=YourFile><BR><BR> <input type=submit name=submit value="Upload"> </form> <% Dim objUpload, lngLoop If Request.TotalBytes > 0 Then Set objUpload = New vbsUpload For lngLoop = 0 to objUpload.Files.Count - 1 'If accessing this page annonymously, 'the internet guest account must have 'write permission to the path below. objUpload.Files.Item(lngLoop).Save "c:\Newupload\" Response.Write "File Uploaded" Next End if %>
- Process_DB.asp
- This is another simple file to get multipart/form-data but this will save the data in the Database
- conn.asp
- This is for using SQLServer to store the files. I have provided the Script to create the Table. So you can create the same table format and use it. Don't forget to fill in the correct Connection String.
- Download.asp
- This file is used to retreive the binay data from the Database and to display on the Web.
CreateTextFile
method. This is not working properly in the case of Excel and other binary files. So limit your usuage only with plain text file.
When the file is uploaded to database, the content type is noted and the binary content is saved as Image data type in the SQL Server. Using download.asp the binary data is written back to the client with the proper content type. So there is no problem with the Database File Upload&download. You can upload all the file types (Excel,jpg etc..) using Process_DB.asp.