ASP.NET – Native Upload

May 8, 2006 at 10:07 pm 3 comments

To faciliate uploading in regular ASP, one used to be reliant on a third-party control such as SAFileUpload, ASPUpload or aspSmartUpload.  Those controls can certainly still be utilized in ASP.NET.  In addition, .NET includes its own upload mechanism.  Here is a quick overview on Adding an Upload Page, Security Considerations and File Size Considerations in Internet Information Services 6.0.

Adding Your Upload Page

  1. In your toolbox, you want to select the File Field under the HTML section.
    File Field
  2. Once that File Field is added to your page, right click on it and select "Run as a Server Control"
    Run as Server Control
  3. Fill in the name and ID accordingly.  In my sample, I named it flUpload.
  4. Add a normal Web Control Button to your form and set up the properties as you see fit.  In my case, I made the Text "Upload" and the name "btnUpload"
  5. I like to add a Web Control Label to the form as well so I can easily report back errors.  In the sample, it is called "lblErrorText" and by default is not Visible.
    Final Form
  6. In order to faciliate the upload, you will need to set your form to have an encryption level.  Switch over to the HTML view of your form.  Add enctype="multipart/form-data" to your form tag.  The final tag will look like this: <form id="Form1" enctype="multipart/form-data" method="post" runat="server">
  7. Now we just need to handle the server code to upload the form.  In my example, I went ahead and and triggered this off the Click event for my button.  Off of our File Field (flUpload), there is a PostedFile property.  If no file was transmitted via the form, the flUpload will be null/Nothing.  If it isn't null, then there are additional properties off of PostedFile which will allow us to retrieve information such as the ContentLength, the ContentType and the FileName (which is actually the full local path to the file).  Of particular interest would be the flUpload.PostedFile.SaveAs method.  This allows you to physically save the uploaded file to the specified path and filename on the server.
    Click Code

Security Considerations

The first time you run the above code, you may have a permissions error reported:
Access Denied

What this means is your ASPNET account does not have the proper permissions to write files to the specified directory.  Instructions to do so are below:

  1. On your web server go into Windows Explorer
  2. Browse to the directory you wish to save the uploaded files to.
  3. Right click on the Directory and go to Properties.
  4. Click on the Security tab.
  5. Click on the Add button.
  6. Type in ASPNET and hit the Check Names button. 
  7. Hit OK
  8. Once you return to the Security Properties screen check "Write" access for the ASPNET user.
    Security Screen
  9. Apply your changes.

IIS6 File Size Considerations

IIS 6 has a limit on the maximum number of bytes that can be contained in an ASP request.  By default, that limit is about 4 MB.  Since we are now transmitted a file via the page's form– our ASP Request size is going to be larger than the normal page.  When you are upload large files, you'll run into errors.  Luckily, a setting can be changed in the IIS6 metabase to facilitate larger uploads.  Instructions are below:

  1. Open IIS (Start->Programs->Administrative Tools-Internet Information Services)
  2. Right-Click the server name and select Properties
  3. Check the Enable Direct Metabase Edit box and hit Apply
  4. Next browse to and open the Metabase.xml file. It is typically located in c:\Windows\System32\inetsrv directory.  You can use Notepad to modify this file.
  5. Search for “ASPMaxRequestEntityAllowed” and change its value to “1073741824”
  6. Save changes in Notepad and exit.

Other References

4GuysFromRolla.com – Uploading in ASP.NET
TheCodeProject – File Upload in ASP.NET
ASPMaxRequestEntityAllowed Description in Microsoft TechNet

Entry filed under: ASP.NET, ASPMaxRequestEntityAllowed, IIS 6, Web Development.

Journal Excerpt: An Evening with Talib Kweli Journal Excerpt/Dad Story: The Color of Mommy’s Hair

3 Comments Add your own

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Flickr Photos

3D Printed Products

Tweets


%d bloggers like this: