File Uploading with .NET
page 1 of 1
Published: 25 Oct 2003
Unedited - Community Contributed
Need to upload a file using .NET? A comprehensive look at the HttpPostedFile class, .NET's built-in file uploading component.
by Damian Manifold
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 20603/ 29

File Uploading with .NET
File uploading with .NET
by Damian Manifold

One of the new additions in .NET is the ability to upload files without the need for additional components. The class HttpPostedFile deals with posted files. It has only a few members, but these cover anything you would want to do with an upload. Here is an example that hopefully demonstrates everything you need to know.

Source Code
Upload.aspx File upload example
Upload.aspx.vb Code behind

The one thing that you must remember when uploading files, is to set the encoding type. If you do not, it will not upload the file.

  4  <form id="Form1" method="post" runat="server" enctype="multipart/form-data">
Upload.aspx sample 1 Generated using CodeView

So how easy is it to up load the file?
Very easy, it can be done in just one line.
In the supplied example, the class is copied into a variable for clarity as it will be referred to numerous times, so it takes three lines.

  11   Dim File as HttpPostedFile
Upload.aspx.vb sample 1 Generated using CodeView
  21   File = Request.Files.Get(0)
Upload.aspx.vb sample 2 Generated using CodeView
  30   File.SaveAs(Server.MapPath("..\..\files\upload\deleteme.txt"))
Upload.aspx.vb sample 3 Generated using CodeView

Note: The user ASPNET must have write access to the target directory if you wish to save the file.

A nice feature of the class if that the files size and type are available without having to save the file to disk.
In the example this is used to stop large files and files that cannot be displayed in the page.

  23  if File.ContentLength > 1024 Then
  24      ErrorMsg.Text = "File is bigger than 1k"
  25  else
  26      if File.ContentType <> "text/plain" Then
  27          ErrorMsg.Text = "File is not a text file"
Upload.aspx.vb sample 4 Generated using CodeView

The ContentType is set using the files MIME type, not the extension and recognizes most popular file formats.

So, all you want to do is process the file for the user or put it into a database, why bother saving it if you do not have to. The class gives you access to the upload stream which can be read just like any stream.

  35   Dim sr as System.IO.StreamReader
  36   Dim strContent as string
  37   sr = new System.IO.StreamReader(File.InputStream)
  38   'display contents
  39   FileContents.Text = sr.ReadToEnd
  40   sr.Close()
Upload.aspx.vb sample 5 Generated using CodeView

Note: If you wish to access the uploaded file as a stream and also want to save it to disk, save it first, as there is a slight lag when closing the stream which may cause the saved file to be empty.

Upload.aspx File upload in action

File upload script class (classic ASP) By Michiel van Otegem

HttpPostedFile Class What Microsoft has to say

User Comments

Title: File Upload   
Name: Nithya
Date: 2010-06-04 1:01:30 AM
Great article.
I am very new to web apps and struggled hard to find pointers to the client upload feature.

Thanks a ton !!
Title: File Uploading with .NET   
Name: parvinder singh
Date: 2010-05-06 6:12:35 AM
Unable to download upload.aspx file, the file is required urgently
Title: AJAX   
Name: Chris
Date: 2010-04-01 11:28:40 AM
Thanks a lot for the thread, it's a great help and it worked fine.. but now I need to upload via AJAX call to aspx.. I'm linking directly to the aspx and the code behind is within the page(no aspx.vb)...

request.files.get(0) is not working, ajax is POSTING the form.. any ideas??

Title: help   
Name: din_student
Date: 2006-11-23 4:33:29 AM
Ermm. .
Sorry to bother. . .

I need help.
How to create the browse button?

and why is strContent not being used?

Plz help. . .
Name: krunal
Date: 2006-09-20 2:51:24 AM
this is easy..

could u tell me how to show an animated gif when file is uploading.... and close it when file uploaded correctly.

Title: File uploading with .Net   
Name: HADI
Date: 2006-06-29 12:12:38 PM
Thanks alot for make it easy to find i tried very hard to find like this ex thanks alot to make it easy to find
Title: Thanks a lot   
Name: Vishal Paterya
Date: 2006-04-11 5:24:09 PM
I m really greatful to you.........thank you Mr.Damian Manifold. One thing I want to ask what for is Request.Files.Get(0)please tell me...
Title: Mr.   
Name: Jeppe
Date: 2006-03-29 8:33:15 AM
Hi, Why is File = Request.Files.Get(0) empty?
Title: Uploading File with .net   
Name: Danel Buttigieg
Date: 2005-08-27 7:22:20 AM

This code has been a great help! Thanks
Title: File uploading with .NET   
Name: takezo
Date: 2005-08-05 5:41:29 AM
Thx for that info, but how can i upload an .pdf file that still will work, im able to upload images, .txt, and they still work, but the .pdf files are corupted

So whats the problem ?

please contact me at springer[AT]
Title: Nice...   
Name: CasinoWinner
Date: 2005-07-25 7:28:56 AM
Thanks for the info....
Title: File Uploading with .NET   
Name: Karen
Date: 2004-12-08 7:04:04 AM
Couldn't find this info anywhere else, it's been a great help. Thank you
Title: File uploading with .Net   
Name: netidas
Date: 2004-09-30 5:03:04 PM
Great info.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-06-25 11:25:20 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search