Uploading Files with ASP

The upload form

Now that we have created the VB ActiveX DLL component, we can code the uploadcomplete.asp file. Add the following code to uploadcomplete.asp, and we'll take a look at what exactly is going on.

<%
Option Explicit
Dim objUpload, strUploadPath, strMsg, strFileExtensions

'create the ActiveX object
Set objUpload = Server.CreateObject("ASPUploadComponent.cUpload")

'set the upload path
'***You will need to change this!***
strUploadPath = "C:\inetpub\wwwroot\uploaddemo\files\"

'set the file extensions to exclude
strFileExtensions = ".exe;.dll"

If objUpload.FieldExists("thefile") = False Then

    Response.Write "Invalid Post Data"
    Response.End
Else
    'file posted...
    'attempt to save the file
    On Error Resume Next
    objUpload.Form("thefile").SaveFile strUploadPath, objUpload.Form("thefile").Value, strFileExtensions
    If Err Then
        'an error occured... ie file already exists, invalid extension etc
        strMsg = "Error " & Err.Number & ": " & Err.Description
    Else
        'add description to the database?
        'cConn.Execute ("INSERT INTO mydocs (FileName,Description) VALUES ('" & objUpload.Form("thefile").Value & "','" & objUpload.Form("description").Value)
        strMsg = "The file was successfully uploaded."
    End If
End If
%>
<html>
<head>
<title>VB Web ASP File Upload Complete</title>
</head>
<body>
<p><%=strMsg%></p>
<p><b>File Name:</b> <%=objUpload.Form("thefile").Value%><br>
   <b>File Size:</b> <%=objUpload.Form("thefile").FileSize%><br>
   <b>Content Type:</b> <%=objUpload.Form("thefile").ContentType%><br>
   <b>Description:</b> <%=objUpload.Form("description").Value%></p>
</body>
</html>

So, lets have a quick look at how this code works. First, we create an instance of the ActiveX DLL using

Set objUpload = Server.CreateObject("ASPUploadComponent.cUpload")

(as we can't add a reference to the DLL like we would in Visual Basic). The next two lines set the path on the server where the file is going to be uploaded, and we set strFileExtensions, specifying the file extensions to exclude. (This is always a good idea, as you don't want your users uploading executables to your server!).

Next, we check to see if the field, 'thefile', exists, using the FieldExists property exposed by the objUpload object:

If objUpload.FieldExists("thefile") = False Then

If it doesn't, we know the form has not been posted correctly, or the user has not selected a file. If it does exist, we try to save the file:

objUpload.Form("thefile").SaveFile strUploadPath, objUpload.Form("thefile").Value, strFileExtensions

The SaveFile method uses the following parameters:

SaveFile UploadFolder, FileName, [ExcludeFileExtensions], [MaxByteCount]

If an error occurs, we trap the error, and otherwise, tell the user the file has been uploaded. At this stage, we could also add the description field to a database, for example. The rest of the page outputs the HTML, and displays the information exposed about the file that was uploaded...

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Before software should be reusable, it should be usable.” - Ralph Johnson