How Do I Upload Files In ASP.NET 2?
Update!! 05-24-2006 - Updated the old beta code to the RTM version of ASP.NET 2.0. Also updated the downloadable source file.
Uploading files in ASP.NET is easier than ever. All you need to do is add the new <asp:FileUpload> control to your page and write a bit of code that deals with the uploaded file once the page is posted back to the server.
To test out the new file upload control, create a new page in Visual Studio 2005. You can choose between a C# or a VB.NET version of the page. I'll be using C# in this example, but the concepts easily translate to VB.NET as well. Select the option to place the code in a separate file.
Once you have created the file, drag a FileUpload control, a Button and a Label onto the page. Set the Text property of the button to Upload. Since Visual Studio 2005 now supports adding controls from the toolbox to the code view, you don't necessarily have to drag the controls to the design view anymore; you can drag them directly into code view as well, right where you want them. No more switching to design view, and no more messed up code when you return from design view!
Once the controls are in place, the code for your page should look similar to this:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DefaultPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>File Upload Demo</title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" Runat="server" /> <asp:Button ID="Button1" Runat="server" Text="Upload" /> <br /> <br /> <asp:Label ID="Label1" Runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
Next, switch to design view (Shift+F7) and double-click the button. You'll be taken to the code behind for the file. Add the following code to the Button1_Click handler that has been added for you:
protected void Button1_Click(object sender, EventArgs e) {
if (FileUpload1.HasFile) { if (!System.IO.Directory.Exists("C:\\MyUploads")) { System.IO.Directory.CreateDirectory("C:\\MyUploads"); } FileUpload1.SaveAs("C:\\MyUploads\\" + FileUpload1.FileName); Label1.Text = GetFileSpecs(); }
}
The next step is to save the uploaded file to that folder using the convenient SaveAs method of the FileUpload control.
Next, add the code for the GetFileSpecs method:
private string GetFileSpecs() { string fileSpecs = "File<br />"; fileSpecs += "Size " + FileUpload1.PostedFile.ContentLength + "<br />"; fileSpecs += "Type " + FileUpload1.PostedFile.ContentType + "<br />"; fileSpecs += "Original path and name " + FileUpload1.PostedFile.FileName + "<br />"; fileSpecs += "Name " + FileUpload1.FileName + "<br />"; return fileSpecs; }
The GetFileSpecs method is a simple helper method that displays some useful information about the upload file. The uploaded file exposes properties like the ContentType, the original path and name of the file that was uploaded.
That's really all there is to it. Save the page, and press F5 to open it in your browser. Click the Browse button, select a file and then click the Upload button.
If all went as planned, the file has been saved to C:\MyUploads with its original name. The label on your page has been updated, and is now showing the details of the file that has been uploaded.
Things To Watch Out For
The SaveAs method of the FileUpload control will overwrite existing files with the same name without warning. To prevent this from happening, be sure to generate a unique file name. One way to do this is to generate a filename from the current date and time. Alternatively, you can rename the files with a sequential number, or prefix them with a unique key, like the user's ID.
To save the files to disk, the account that the Web server is running under needs permission to write to the specified disk and folder. Be sure to check out this FAQ to find out how you can set the required security settings.
Download Files
Source Code for this ArticleWhere to Next?
Wonder where to go next? You can post a comment on this article.
Links in this Document
Doc ID | 323 |
Full URL | https://imar.spaanjaars.com/323/how-do-i-upload-files-in-aspnet-2 |
Short cut | https://imar.spaanjaars.com/323/ |
Written by | Imar Spaanjaars |
Date Posted | 10/02/2004 23:49 |
Date Last Updated | 05/24/2006 22:10 |
Date Last Reviewed | 05/24/2006 22:10 |
Listened to when writing | Cochise by Audioslave (Track 1 from the album: Audioslave) |
Comments
Talk Back! Comment on Imar.Spaanjaars.Com
I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.
When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.
If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the Doc ID of the document.