File upload to the server
File upload to the server
By Michiel van Otegem
10 November 2000
Uploading a file from the users computers to the webserver is simple
when you use a HTML-form (HTML version3.2 and higher) and ASP. On the computer of the user
no extra software has to be installed. On the server however there has to be
software present that can handle the file. There are approaches that require only script (see
File upload script class), but due to perfomance
considerations it is not suitable for high-volume sites.
For the software on the server there are multiple possibilities. With Internet Information Server 4.0
the Posting Acceptor is supplied, this however is difficult to use, so most programmers use an alternative.
The most used upload component is
SA-FileUP by
Software Artisans,
of with which sample code below works. Other alternatives are
AspUpload by
Persits Software and
ASP File Upload (free) by
Caprock Consulting.
These all work similar to SA-FileUP, so using those instead is not hard.
The HTML-form with which the upload is done, has to contain two parts:
- The form-attribute enctype="multipart/form-data", as follows:
<FORM ENCTYPE="multipart/form-data" METHOD="POST" ACTION="uploadsimple.asp">
This is so the server knows that a different type of form than normal is being used, within which
files can be send (this is not mandatory however).
- One or more form-fields of the file, as follows:
<INPUT TYPE="FILE" NAME="file1">
In the ASP-page where the form is sent to, the FileUp object will be created. Then the file is saved in the
appropriate folder and the object closed again. Before the file is saved we specify that the file can't be larger than 15 kB.
When this isn't done, it's possible to send a file of 2 GB with SA-FileUp.
Keep in mind that if you don't use a component, you can't get the files unless you use a component or the script
mentioned earlier. The form's data can be read as binairy data, but then the Request.Form collection is unusable,
and the data has to be manipulated in script to get the files and the values from it (which isn't easy).
This means the form-value description isn't available in the Form collection
of the Request object and therefore has to be read through via the Form the code you have to create to disect the
binairy data. SA-FileUp (and the other components) do this for you, but you do need to get the data from ALL form
fields from the object.
Sample HTML
<html>
<body BGCOLOR="#FFFFFF">
<form METHOD="POST" ENCTYPE="multipart/form-data" ACTION="uploadsimple.asp">
File: <input TYPE="FILE" NAME="file1"><br>
Description: <input TYPE="TEXT" NAME="description"><br>
<input TYPE="SUBMIT" VALUE="Send file">
</form>
</body>
</html>
Sample ASP
<%
Option Explicit
Dim oUpload
Dim sUserFilename, sDescription
Dim lTotalBytes
Set oUpload = Server.CreateObject("SoftArtisans.FileUp")
oUpload.MaxBytes = 15000
oUpload.Path = Server.Mappath("upload")
oUpload.SaveAs "upload1.tst"
lTotalBytes = oUpload.TotalBytes
sUserFilename = oUpload.UserFilename
sdescription = oUpload.Form("description")
Set oUpload = Nothing
%>
<HTML>
<BODY BGCOLOR="#FFFFFF">
<%
Response.Write "file: " & sUserFilename & "<br>"
Response.Write "Number of bytes: " & lTotalBytes & "<br>"
Response.Write "description: " & sdescription
%>
</BODY>
</HTML>
|