In this example, we are going to create a very simple page
that accepts an upload via a form and puts it into a directory
on your server.
Note: The directory /Uploads should be created in your web
server root before running this example. Also, be sure to have
the permissions set for this new directory to allow "Everyone"
read/write access. If you are going to copy and paste this
text, please remember to remove the line numbers. You can
download a set of example forms in a zip file
here.
Code:
| Line # |
HTML |
| 1 |
<% |
| 2 |
Option Explicit |
| 3 |
|
| 4 |
Dim upl, NewFileName |
| 5 |
|
| 6 |
Set upl =
Server.CreateObject("ASPSimpleUpload.Upload") |
| 7 |
|
| 8 |
If Len(upl.Form("File1")) > 0 Then |
| 9 |
NewFileName = "/Uploads/" &
upl.ExtractFileName(upl.Form("File1")) |
| 10 |
If upl.SaveToWeb("File1", NewFileName) Then |
| 11 |
Response.Write("File successfully
written to disk.") |
| 12 |
Else |
| 13 |
Response.Write("There was an error
saving the file to disk.") |
| 14 |
End If |
| 15 |
End If |
| 16 |
%> |
| 17 |
|
| 18 |
<html><head><title>ASP Simple Upload Example
#1</title></head></title> |
| 19 |
<body> |
| 20 |
<form method="POST" action="Example1.Asp"
enctype="multipart/form-data"> |
| 21 |
Select a file to upload: <input type="file"
name="File1" size="30"> |
| 22 |
<input type="submit" name="submit" value="Upload
Now"> |
| 23 |
</form> |
| 24 |
</body> |
| 25 |
</html> |
| Line # |
Description |
| 1 |
Starts the ASP code section of the file. |
| 2 |
Forces us to declare all of our variables. |
| 4 |
Declares the variables that we're going to use in the
code. |
| 6 |
Create an instance of the Upload component so we can
use it throughout the code. |
| 8 |
Check to see if the user uploaded a file. If they did,
this will contain the name of the file on their
hard drive. Also, note that we us the component to get the
form fields, not the Request object. You must use the
component to read the form variables in all of the
forms with file uploads on them. |
| 9 |
Set the NewFileName variable to the /Uploads/
directory in our web server root and append just the name
of the file that was uploaded. |
| 10 |
Save the file to the our web server. This method
automatically calls the Server.MapPath for you so you
don't have to figure out where your website is physically
located. The first parameter of SaveToWeb is the field
name (File1) and the second parameter is the location and
filename you want to save this file to. |
| 11 |
If the save was successful, we tell the user. |
| 13 |
Otherwise, tell them the upload failed. |
| 16 |
End the ASP part of the page. |
| 20 |
Start our form making sure to set the method to POST
and the enctype to multipart/form-data. |
| 21 |
Add the file input tag to the form with the type of
file and the form field name of File1 and a form field
length (on the screen) of 30. |
| 22 |
Add the submit button to the form so that after they
select a file to upload they can submit it to this page
for processing. |
| 23 |
End the form |