by Jacob Gilley
As a regular to ASP 101, I feel it neccesary to give a little back to the site that keeps me on top of things in the world of ASP. And so, I give you the FileUploader ASP Library. In a nutshell, the FilerUploader library is simply a single ASP include that provides an object-oriented approach to getting files from Internet clients.
I have seen numerous “pure ASP” upload solutions, some you even had to purchase, that did the job, but required you to wade through pages of code just to see how things worked. Then there are the COM components that you can buy that do what you want and more, but cost an arm and leg to obtain. What I wanted was something simple, centralized, easy to use and FREE! The FileUploader Library was the solution and now I want to share my work with fellow developers.
NOTE – YOU MUST HAVE VBSCRIPT v5.0 INSTALLED ON YOUR WEB SERVER FOR THIS LIBRARY TO FUNCTION CORRECTLY. YOU CAN OBTAIN IT FREE FROM MICROSOFT WHEN YOU INSTALL INTERNET EXPLORER 5.0 OR LATER.
The library consists of two VBScript classes: FileUploader and UploadedFile. The FileUploader is the top-level object in the model and is the only one that you will need to instantiate.
Here is how to do it: (this assumes that you have included “upload.asp”)
<%Dim
MyUploader
Set
MyUploader =
New
FileUploader
%>
Now once your FileUploader object is initialized, you can begin recieving the uploaded data from the client by a single call to the Upload() method.
<%
MyUploader.Upload()
%>
Or, since Upload() is the default method, you can use a shortcut and start the upload process like this: (Use one or the other, not both)
<%
MyUploader()
%>
Once that is complete, you can begin access and saving the file to your hard-drive or to a database. The uploaded files are accessed through the Files Collection of the FileUploader object. The Files Collection is a set of UploadedFile objects that represent each file uploaded.
Here is an example of how to enumerate the Files Collection and write each uploaded files’ information to the browser:
<%Dim
File
For Each
File
In
MyUploader.Files.Items Response.Write "File Name:" & File.FileName Response.Write "File Size:" & File.FileSize Response.Write "File Type:" & File.ContentType
Next%>
Here is an example of how to access a specific file in the Files Collection using the HTML file input element name as the Index:
<%
Response.Write "File Name:" & MyUploader.Files("file1").FileName Response.Write "File Size:" & MyUploader.Files("file1").FileSize Response.Write "File Type:" & MyUploader.Files("file1").ContentType
%>
For simplicity sake, I will be the using the For Each…Next variation for the following samples. Now, all the files are uploaded and its time to put them somewhere. There are two places you can save the uploaded files…on your hard-drive or to a database.
Here is how to save the files to disk:
<%Dim
File
For Each
File
In
MyUploader.Files.Items File.SaveToDisk "C:UploadedFiles"
Next%>
And how to save to a database: (Replace “MyUploadTable”, “CONNECT STRING..” and field names accordingly)
<%Dim
RS
Dim
File
Set
RS = Server.CreateObject("ADODB.Recordset") RS.Open "MyUploadTable", "CONNECT STRING OR ADO.Connection", 2, 2
For Each
File
In
MyUploader.Files.Items RS.AddNew RS("filename") = File.FileName RS("filesize") = File.FileSize RS("contenttype") = File.ContentType File.SaveToDatabase RS("filedata") RS.Update
Next
RS.Close
%>
I think that should just about cover everything. I hope this helps out and that you found my blathering somewhat interesting. If you have any questions or comments about this article or the code provided, feel free to email me. ([email protected])
You can download the library and sample files below (4 KB).
p34c3 +0 4|| c0d3rz!