
Uploading files from a web page to a server is quite a sought after feature in a web site to some developers. Sometimes this basic functionality is essential. For example, this web site uses file uploading to the users to submit their tutorial for review.
However, file uploading is not as easy as it might sound, especially with Active Server Pages because there is no standard component that can be used. There are a lot of companies that have special custom made components, (usually COM objects), for sale that you can use but they are often extremely costly.
Whilst searching online, I came across a script that allows you to upload files to a web server without the use of a COM Object. This was an extremely handy piece of code. After looking through the code you can see that the script works by using the server FileSystem object to write the uploaded data directly the servers hard drive.
You can download the code from here:
Download ASP Upload Source Code Class
There are a number of files in the above zip file, the main file you need being the clsUpload.asp file. This file contains the class definition used by the script for uploading. There are also two example files, one called 'ToFileSystem.asp' and one called 'ToDatabase.asp'.
As these two files indicate by their names, these files use a form to upload a file from the clients hard driver to either the server in the form of a file somewhere, or it uploads the data into a database (also provided in the zip file). The code is very easy to use for the FileSystem version although some modification will be needed to suite your needs.
Remember that because clsUpload class uses to 'BinaryRead' function to retrieve the submitted form data, you will not be able to use the Request.Form or Generic Request methods to get at other form element data. The best way to deal with this is to have a seperate form that references the file after it has been uploaded.
For example, the N2K Resource Center Tutorial Submission System requires that the user first upload their files, then enter the details for the uploaded tutorial after it has been uploaded.
Some modifications you may need to make to the example 'ToFileSystem.asp' file are:
Wherever you want users to upload files to on your web site, you will need to ensure that the desired folder has write permissions enabled on it. I would also disable to the execute permission on the folder to stop people from uploading files and then executing them on the server as this could be used by hackers to retrieve server information or even execute a virus.
This article isn't so much a tutorial as a solution to uploading files without a COM object. The example files in the zip file above provide ample information to get basic uploading features onto your web site with minimum fuss.
If you have any problems, then just drop a line in at the N2K Forums.
Tutorial by Justin Kercher
2005.
This document may not be copied for use on another site.
Copyright© 2005.