ASP file upload example using csASPUpload

This demonstration shows how to capture files that are uploaded in an html form and save them to disk on the server using classic ASP. The files needed can be downloaded below and they consist of the sample scripts as well as the trial version of the csASPUpload component.

Download the sample ASP scripts - filesave.zip (40 KB)

Download the trial component - csupt.zip (2.2 MB)

There are 4 web pages which must all be put in the same folder. A sub folder must be created below this folder with the name "files". The Internet Guest User must have Modify permission on this sub folder because the demo will need to be able to create and delete files. The demo assumes the 32 bit trial version of csASPUpload, is being used although it can easily be modified to use the 64 bit or full version. The files used are listed below.

File Description
fileupload.htm The starting page. It contains an html form for uploading a file.
filesave.asp This is the script that handles the form data. It uses the csASPUpload component to save the file.
view.asp This script uses the File System Object to list the files already uploaded.
delete.asp This script uses the File System Object to delete selected files that have been uploaded.

Some of the key points will be explained here, although a description is given in the readme.pdf file.

fileupload.htm

This is the starting page. It contains an html form with an input type of file. It is important to set the "enctype" attribute:

<form method="post" action="filesave.asp" enctype="multipart/form-data">
<input type="file" name="filesent">
<input type="submit" value="Send File">

filesave.asp

This is the script that uses the csASPUpload component to save the file. First the instance of the component is created. This line must be modified if you use the 64 bit trial or the full version.

Set Upload = Server.CreateObject("csASPUpload32Trial.Process")

The main part of the script consists of an If..Then..Else construct which checks if a file has been uploaded and saves it. If no file was attached it shows a message. It is recommended to always use the FileQTY property to check for a file before attempting to process it.

<%
If Upload.FileQty > 0 Then
  Upload.FileSave Server.MapPath("./files/") & "\" & Upload.FileName(0), 0
%>
  <p>The file has been stored</p>
  <p><a href="fileupload.htm">Upload</a> another file.</p>
  <p><a href="view.asp">View</a> files already uploaded.</p>
<%
Else
%>
  <p>No file was received</p>
  <p><a href="fileupload.htm">Upload</a> another file.</p>
  <p><a href="view.asp">View</a> files already uploaded.</p>
<%
End If
%>

The FileSave method takes two parameters, the physical path of the file and the index of the file. This index will always be zero if files are uploaded one at a time. If multiple files are uploaded it will vary. This example uses the same name as the uploaded file but care must be taken not to overwrite an existing file. Some more logic may be required at this point depending on what your application does.

view.asp & delete.asp

These two scripts are included to make the demo application more usable. view.asp links to each file in the "files" directory and provides a delete button for each file. It is worth noting that there could be a security risk here as an ASP file could be uploaded which could then be run from this page. In an application used by the public the permissions would need to prevent this.

Cookies

This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.