We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 128,499 times

Contents

Related Categories

Uploading Files with ASP - The upload form

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...

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Invalid class string error

    Posted by selvass on 19 Apr 2005

    When i call to component in asp, when i run the uploaddemo.asp, i choose one file, once i press uploading
    showing at Invalid class string error. please help me this how to solve this.

  • A progress Bar for the Upload Component

    Posted by haguila on 12 Apr 2005

    Hi:
    The component is really good, the code works fine and it has not any problem when is impersonated via MTS.
    So, congratulations.
    From my ignorance the problem is the Event Blindness: I cannot mo...

  • Doubt

    Posted by livinvarghese on 03 Dec 2004


    Hello,

    how i can upload an Excel file which having the file Size 6MB by ASP.NET to the database.

  • ASP and VB DLL Debuggubg

    Posted by sunnysuku on 01 Apr 2004

    Please Help me !!
    How can I debug ASP pages and VB compenets . Ie I have ASP pages in Visual Interdev. These pages talking with few DLLS. I have the VB Source code for these dlls also. Please tell me...

  • what are the components of ASP?

    Posted by preetha on 01 Mar 2004

    What are the components of ASP and VB? Pls help me.