AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=234&pId=-1
File Uploading with .NET
page
by Damian Manifold
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 20894/ 13

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.

Examples
Upload.aspx File upload in action

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

Links
HttpPostedFile Class What Microsoft has to say


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 1:29:32 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search