Learn how to work with FileUpload Control | Learnxpress

Learn how to work with FileUpload Control

February 9th, 2009 Anand Narayanaswamy 1 Comment

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, 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 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.

Notes

(1) Visual Studio 2005 is not compulsory for working with the above example. Just download the code files from the links given below and 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. This process requires access to the remote server. 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.

Summary

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

 

If You Like This Post, Share it With Your Friends & Peers

Digg
stumbleupon
Delicious
facebook
twitter
reddit
rss feed bookmar
   

One Comment to “Learn how to work with FileUpload Control”

  1. The length of the article seems to me not to be the problem. I tend more toward my speakers Alice, trying in your posts, etc. to incorporate more food for thought. It seems likely that the user can not afford to feel this prompted the “discussion” contribution.

Leave your comment here:

Terms

  • asp net alterar propriedade text do fileupload
  • code of fileuploading in c# net3 5
  • visual studio 2010 FileUpload

  • file upload control in asp net3 5
  • file upload is not working for trigger in asp net3 5
  • propriedades fileupload c#
  • FileUpload1 HasFile
  • Visual Studio FileUpload
  • mudar texto fileupload asp net
  • ASP NET 4 0 FileUpload
  • alterar propriedade fileupload c#
  • winforms fileupload
  • silverlight 3 upload control
  • fileupload silverlight 3
  • agregar fileupload a las herramientas de c sharp
  • visual studio 2008 file upload control
  • fileupload1 visual studio 2005
  • toolbox vs 2005 does not have FileUpload control
  • file upload control in winform

  • visual studio 2010 file upload
  • seo forum


Google Analytics Alternative