ASPNL logo (1 kb)
Thursday, November 20, 2008

powered by


Microsoft ASP.NET Connections
Member of ASP Guild
<< previous | index | next >>
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
<%
'//Al variables have to be declared
Option Explicit

'//Declare variables
Dim oUpload
Dim sUserFilename, sDescription
Dim lTotalBytes

'//Use FileUp object
Set oUpload = Server.CreateObject("SoftArtisans.FileUp")

'//File can be maximum 15 kB
oUpload.MaxBytes = 15000

'//Decide in which folder the files are placed
oUpload.Path = Server.Mappath("upload")

'//Save the file if upload1.tst
oUpload.SaveAs "upload1.tst"

'//Request the total amount of bytes of the file
lTotalBytes = oUpload.TotalBytes

'//Request the name of the file
sUserFilename = oUpload.UserFilename

'//Use Form collection of the upload component
sdescription = oUpload.Form("description")

'//Ruim object op
Set oUpload = Nothing
%>
<HTML>
<BODY BGCOLOR="#FFFFFF">
<%
'//Write result to the browser
Response.Write "file: " & sUserFilename & "<br>"

Response.Write "Number of bytes: " & lTotalBytes & "<br>"
Response.Write "description: " & sdescription
%>
</BODY>
</HTML>

<< previous | ^ to top | index | next >>
copyright 2000-2002 ASPNL