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,404 times

Contents

Related Categories

Uploading Files with ASP - Getting Started

Getting Started

Before we begin, create a new folder called 'uploaddemo' in your C:\inetpub\wwwroot directory (or where-ever else your files are stored for IIS). Create a new file in there called upload.asp, and another called uploadcomplete.asp. Leave these files empty for now.

Now, create another directory within 'uploaddemo' called 'files'. This is where our uploaded files are going to be stored. In order for this to work, you need to allow IIS to be given write access to this directory. To do this, go to the Internet Information Services console, select the appropriate web, and then display the properties for the 'files' directory. In the Directory tab, ensure the 'Write' checkbox is selected, and click OK.

We're now ready to write some code for upload.asp, where the user will be presented with a form to select a file, and give the file a description. Add the following code to the upload.asp file, and then we'll take a closer look at it.

<html>
<head>
<title>VB Web ASP File Upload</title>
</head>
<body>
<form action="uploadcomplete.asp" method="POST" enctype="multipart/form-data">
 <p>Please select a file to upload, and enter a description for it.</p>
 <p><b>File: </b><input type="file" style="width: 300;" name="thefile"><br>
 <b>Description:</b><br><textarea name="description" rows=4 cols=30></textarea></p>
 <input type="submit" value="Upload">
</form>
</body>
</html>

As you can see, the form is currently just plain HTML. However, there are a few differences that you may not have come across before. The first is this:

<form action="uploadcomplete.asp" method="POST" enctype="multipart/form-data">

Although at first glance this is a standard form, the enctype property makes a significant difference! When set to multipart/form-data, the form is not posted in the standard format - you will not be able to access the posted data through the standard Request.Form(FieldName) syntax. However, this format does allow files to be transmitted to the server, as well as form fields. When we create our ActiveX DLL, it will parse this posted data, and save the appropriate part into a new file.

The second difference is the

<input type="file" style="width: 300;" name="thefile">

line, which uses the "file" input type. This input field allows the user to select a file using an Open dialog.

The 'File' input type
When a form is submitted using this input box, the file, rather than the filename is transmitted. However, this does have some security consequences... A website could in theory hide this field, with it's value pre-set, and then when the user transmits the form, he/she unknowingly sends the server a file too. Therefore, both IE and Netscape have disabled the value property for this type of input field. The consequence of this, is if you use server-side validation (for, say, checking the length of the description), when you display the form again with the error, you won't be able to use the input tag's value attribute to remember the file the user already selected. Something to bear in mind....

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.