AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=761&pId=-1
CodeSnip: Working with FileUpload Control
page
by Anand Narayanaswamy
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 13282/ 16

Introduction

Uploading files to a folder on the web server is made easy with ASP.NET 2.0. In this step-by-step article, you will learn how to achieve this task with the help of code samples in C# and Visual Basic .NET.

Requirements

Windows XP Professional SP2 or Windows Server 2003, Visual Studio 2005

Steps

1. Start Visual Studio 2005.

2. Drag and drop a FileUpload control from the Toolbox onto the design area. I didn't modify its ID property, but you can do so if you wish.

3. Drag and drop a Button control and supply the following properties.

ID: btnUpload

Text: Upload

4. Drag and drop a Label control and modify its ID property (Example: lblStatus).

You have to delete the default text by erasing the value of the Text property. You should also modify its ForeColor, as the default color is white.

Note: You can also double-click a control from the Toolbox.

5. Double click the Button control and paste the following code.

Visual C# .NET

if (FileUpload1.HasFile)
{
  try
  {
    lblStatus.Text = "Uploading File " + FileUpload1.FileName;
    FileUpload1.SaveAs("C:\\Files\\" +FileUpload1.FileName);
    lblStatus.Text = "File Successfully Uploaded";
  }
  catch
  {
    lblStatus.Text = "Unable to save the file";
  }
}
else
{
  lblStatus.Text = "You have to select a file to upload";
}

Visual Basic .NET

If FileUpload1.HasFile Then
  Try
    lblStatus.Text = "Uploading File " + FileUpload1.FileName
    FileUpload1.SaveAs("C:\\Files\\" + FileUpload1.FileName)
    lblStatus.Text = "File Successfully Uploaded"
  Catch
    lblStatus.Text = "Unable to save the file"
  End Try
Else
  lblStatus.Text = "You have to select a file to upload"
End If

Make sure that you have created a folder named Files on the C drive before attempting to run this program. Otherwise, the catch block will be executed. If there are no files to upload, then the application will execute the else block.

6. Run the application, browse for a file from your local hard drive, and click on the Upload button. If everything works out fine, then you should be able to view the uploaded file inside the Files directory.

Note:

(1) Visual Studio 2005 is not compulsory for working with the above example. Just download the code files from the links given below, copy them to Inetpub\wwwroot folder, and execute. You need to install .NET Framework 2.0 before running the code.

(2) Sometimes, you will have to give write permission for the folder to which you are attempting to upload the files. If your application is using default accounts, then ASPNET user account needs write permission to the folder to which you are trying to upload the file. If you don't have the access to the web server, then contact your hosting service provider.

(3) You will have to employ a suitable method of authentication before allowing users to upload files. This will prevent unauthorized uploads, which can ultimately harm your server.

(4) The default maximum size of a file to be uploaded to the web server using the FileUpload control is around 4MB. In case if you wanted to upload the file bigger than 4MB, you have to alter maxRequestLength parameter either in web.config or machine.config. For more information, reference the <httpRuntime> Element on msdn.

 

[Code Download - Visual C# .NET]

[Code Download - Visual Basic .NET]     

Summary

In this article, you have learned, with the help of an example, how to upload files with the help of FileUpload control using ASP.NET 2.0.

 


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-18 5:10:54 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search