CodeSnip: Working with FileUpload Control
page 1 of 1
Published: 01 Feb 2006
Unedited - Community Contributed
Abstract
This article demonstrates the working of FileUpload control using ASP.NET 2.0 and C#.
by Anand Narayanaswamy
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 13253/ 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.

 



User Comments

Title: question   
Name: Suryam raju
Date: 2013-01-15 6:39:27 AM
Comment:
How can i insert image in database with out writing stored procedure using fileupload control in asp.net send me code as early as possible plz
Title: image upload   
Name: bala
Date: 2010-07-09 7:46:32 AM
Comment:
its very simple to learn..thanks
Title: Question   
Name: sushama
Date: 2010-02-28 3:38:06 AM
Comment:
I want to know how to write a sql query for insert a image in database without using storeprocedure through fileupload control in asp.net with c#
Title: Iam Having one Doubt   
Name: Aniz
Date: 2009-12-21 6:00:42 AM
Comment:
How to retrieve the path that we stored in the database into the FileUpload Control?
Title: much thanks!   
Name: rob
Date: 2009-04-18 4:44:36 PM
Comment:
Thanks Anand! Really appreciate you taking the time to post this!
Title: Thank you so much   
Name: kal
Date: 2009-03-12 6:27:55 PM
Comment:
thank you so much for the article.
Title: Thanks!   
Name: Some Guy
Date: 2009-02-20 3:28:47 PM
Comment:
This article was a huge help for me in understanding how File Upload controls work.
Title: Fileupload   
Name: nagarjunareddy.s
Date: 2008-12-18 3:51:35 AM
Comment:
very good article
Title: Upload folder   
Name: Sandeep Kaur
Date: 2008-12-11 5:03:09 AM
Comment:
I have a fileupload control,image control,dropdownlist and a button. when i browse then open the some folder to select an image then the full path of image comes in fileupload control but i want to have juat the folder path from which all the image paths come in dropdownlist how to that?
Title: HELP   
Name: Avinash
Date: 2008-11-19 1:38:12 AM
Comment:
I have a fileupload control image control & a button when i click on button browsed image from fileuploadcontrol should display on imagecontrol how to do that
plz help me my mail id is vinash.22@gmail.com
Title: good   
Name: Shindey
Date: 2008-11-08 10:36:58 AM
Comment:
good
Title: Kumar   
Name: Chandan
Date: 2008-09-27 4:13:07 AM
Comment:
I have a fileUpload and image control.when i click on browse button it display the open file dialog then i select an image file.I want,when i click on open button of the open dialog box at that time image box display the image which url display the textbox in fileupload control
plz help me my mail id is chandank540@gmail.com
Title: need ur valuable help   
Name: Shaheen
Date: 2008-09-09 3:34:30 AM
Comment:
Can you please tell how to upload to a server than the local C drive. Suppose my server path is www.anand.com/asp/

pls send a copy of the reply to shaheen97@gmail.com also.

Thanks in advance
Title: good work sir   
Name: sai gopal
Date: 2008-07-25 4:25:50 AM
Comment:
u have done a fantastic work . ur work has been useful in year 2008 also .good work
Title: Fileupload   
Name: Ashik
Date: 2008-06-24 8:02:42 AM
Comment:
hi..
I want to clear the textbox in the file upload control when I click a Reset/Clear button.
plz help us....
Title: Fileupload   
Name: pooja
Date: 2008-06-14 9:17:14 AM
Comment:
I have a problem is that, i want to upload a image in the data base but condition is that if i already have same image in the database than it give me a error like this file already exist.plz find the code in the asp.net 2.0 ,c#
. my email id is jaisika_nhn@yahoo.com
Title: Still Problem...   
Name: Masood
Date: 2008-06-07 2:52:23 AM
Comment:
The code is to save file to a path. Qus is
How to get file path when a file is uploaded
Title: fileupload   
Name: abrar hussain
Date: 2008-04-09 2:51:17 AM
Comment:
Hi sir
I have a problem in imagecontrol,how to save image in data base & display in imagecontrol.Accually I save imagepath in database but how to display image in imagecontrol plz tell me code to access database.my id is abrar_jec06@yahoo.co.in
Title: retain fileupload text   
Name: musta
Date: 2008-03-17 12:07:42 PM
Comment:
i have made a page with a button and a fileUpload. the button has a postback function so when it executes it clears the fileUpload field. is there any way of preventing this as it would be quite bothersome to choose a file each time.or is there a way to write the code written in my button click block somewhere else so that when i select a file and click OK on the browser dialog box it should be executed.plz reply soooooon to mustaali786@yahoo.com.thanks
Title: file upload   
Name: Rohit Nagpal
Date: 2008-02-11 4:18:46 AM
Comment:
hello,how r u?
i wana help, regarding retrieving pic which i uploaded through this control ,i want coding,can u send me coding of
the same on my email id.reply soon.rohitnagpal.mca@gmail.com
ok byeeeee
Title: Upload files into sql developer   
Name: Pooja
Date: 2008-01-16 3:33:32 PM
Comment:
Hi Anand,
Can you let me know how to save the uploaded files in sql developer?
Title: Keep file upload and multifile copy to server   
Name: Ahmed
Date: 2007-12-13 8:30:21 AM
Comment:
Hi Anand,
What maria face the problem, I am also facing. What is solution for this and how upload multifiles? Pls send to my email(ahmedaaki@yahoo.com)
Thanks.
Title: Keep file upload
Name: Maria
Date: 3/26/2007 10:08:42 PM
Comment:
My problem seem similar to Xeyne. However i couldn't find the answer to her question. ive made a page with various controls on it, including the fileUpload. Some of the other controls has a postback function so when they execute it clears the fileUpload field. is there any way of preventing this as it would be quite bothersome to choose a file each time. plz help
Title: Reply to Ilyas   
Name: Anand
Date: 2007-09-22 5:52:58 AM
Comment:
It is possible to call file upload control window using a hyperlink. A nice article will be soon published regarding this subject. So keep visiting ASPAlliance and you will find that article soon.

Author Community Server Quickly http://www.packtpub.com/community-server/book
http://www.amazon.co.uk/exec/obidos/ASIN/1847190871/
Title: Mr   
Name: Ilyas
Date: 2007-09-06 9:08:03 AM
Comment:
Hi,
Is it possible to call File Upload control dialogue window using a hyperlink ?
Title: FileUpload Control Issue   
Name: Rajesh
Date: 2007-08-09 12:29:21 AM
Comment:
Hi,
I am facing issues with FileUpload Control. When I use a File Upload control in my web page the Web page doesnot download reports which it will do fine when there is no File Upload control. I am using WebFocus to download the reports.

What is the relation between the WebFocus and File Upload Control?
IF there is no relation what is the reason for this behaviour?.
I am a beginner for both ASP .Net and WebFocus so some one pease help me.

Rajesh
Title: File Upload Control   
Name: Srenvas
Date: 2007-07-27 2:11:29 AM
Comment:
Hi,
I am actually finding problems when working with file upload control..it works fine on my local machine but when i have deployed the application it gives some security exception..on the web server..i have given all permissions to the folder where i am uploading but still does'nt work can you pls tell me how to come out this issue

Thanks
Srenvas
srenvas_2511@yahoo.com
Title: File upload control   
Name: RaviKumar
Date: 2007-04-13 1:35:45 AM
Comment:
Hi
I want to save the File path in the database(sql server) by using FileUpload control .. can anyone tell me that how that can be done .. Reply to my Id ravikumarmp@rediffmail.com ..


When u click the browse button that particular path should be saved in the database.. and also how to call that contrrol in the insert command..
Title: Keep file upload   
Name: Maria
Date: 2007-03-26 10:08:42 PM
Comment:
My problem seem similar to Xeyne. However i couldn't find the answer to her question. ive made a page with various controls on it, including the fileUpload. Some of the other controls has a postback function so when they execute it clears the fileUpload field. is there any way of preventing this as it would be quite bothersome to choose a file each time. plz help
Title: About this Article   
Name: Biggame
Date: 2007-02-12 9:44:42 PM
Comment:
Can anyone tell me how to hyperlink the ".FileName" or just retrieve the uploaded file please?
Title: File upload   
Name: vikas
Date: 2006-12-28 2:28:54 AM
Comment:
Hi Anand i want to upload a file without using fileupload control, by simply dragging from local to webpage. Can u help me about this issue.
Title: Really good artcle Mr. Anand   
Name: bhargav
Date: 2006-12-04 2:04:27 AM
Comment:
Actually i need code to upload image into database. So i can implement using your article.
thanks for your article
Title: Upload   
Name: Steveo
Date: 2006-11-30 10:28:37 AM
Comment:
Great article I found it helpful and I am a beginner with Asp.net
Title: Mistake   
Name: Ice Latte
Date: 2006-11-20 12:50:38 AM
Comment:
i mean paste "here"
Title: Retrieve image   
Name: Ice Latte
Date: 2006-11-20 12:04:43 AM
Comment:
Hey,i am able to upload image with your codes. .
but i do not know how to retrieve it by using the path in my sql server. .
do anyone know how ?
pls post the code there if u do . .

thanks in advance
Title: Great   
Name: Ice Latte
Date: 2006-11-19 9:11:48 AM
Comment:
Hey Thanks for your codes
Title: about this   
Name: bhagwat
Date: 2006-11-11 3:26:38 AM
Comment:
this is nice but is there is any solution for dotnet 1.1
Title: About this article   
Name: luis (pt)
Date: 2006-11-09 5:27:40 PM
Comment:
thank you, you saved my day!
Title: Next Step   
Name: Mr DJ
Date: 2006-11-07 5:39:09 PM
Comment:
This works great, can you help me with the next step? I need to save my files to a Virtual Directory on another machine on the Domain. Can I still use the same control or do I need something else?

Thanks in advance
DJ
Title: Mr   
Name: Michael
Date: 2006-09-15 3:28:54 AM
Comment:
is it possible then to call those files(say they were images) using a store proc and some search criteria, if so how
Title: File Uploade Bug   
Name: Hussam
Date: 2006-09-14 6:07:45 AM
Comment:
Hi All,

There is a small bug in FileUpload Control,this bug appear when you type rubbish (eg.rhghhyt) in the TextBox of the UploadFile Control you can not activate any thing in the page ,for example in the sample code above try to type any thing in the UploadFile control then click Upload button with out clicking Browse button ..you will see that there is nothing happened there (the error message not appear),
same as if there is any other controls in the page.

Hussam


Is there any one who have a solution for this problem???
Title: Upload Control Problem   
Name: Vijaya
Date: 2006-09-04 10:14:07 AM
Comment:
I have used the file upload control to upload multiple file.
(similar to hotmail).
I used the Atlas to restrict the postback when selecting the files.
each file is adding to listbox and clicking on submitt button it will upload to server.
My problem is .. when i am adding to listbox i am not able to get the fileupload filename.
Is it upload control is not working with Atlas. because of client side?
Please Help Me!!!
Title: informative   
Name: kalyani
Date: 2006-06-26 2:25:12 AM
Comment:
this article is good.
Title: Multiple File Upload   
Name: GoodGirl
Date: 2006-06-22 9:44:20 AM
Comment:
Is it possible to upload multiple files using fileupload control?
Title: Keep file upload   
Name: Xeyne
Date: 2006-05-10 10:42:31 AM
Comment:
ive made a page with various controls on it, including the fileUpload. Some of the other controls has a postback function so when they execute it clears the fileUpload field. is there any way of preventing this as it would be quite bothersome to choose a file each time. plz help
Title: not declared   
Name: newbie
Date: 2006-05-09 2:36:41 AM
Comment:
Hi, the code works fine on its own but when i try to incorporate it to my aspx page with headers and navs, i am getting this message:
Compiler Error Message: BC30451: Name 'FileUpload1' is not declared.
Please help. Thanks!
Title: hai   
Name: sakthi
Date: 2006-04-15 5:17:22 AM
Comment:
very usefull for this article. send the coding how to download the file also.
Title: No filter for browse in control   
Name: Ken
Date: 2006-03-17 11:38:13 AM
Comment:
Seems that MS has started counting the horses after the barn door has been left open with the UpLoad control. The browse has no means of providing a filter mask, therefore a client can attempt to select any number of files/ext, and the only thing that can be done is to check it after they've selected it. In my mind this is bad form. There should be an attribute of the browse that would allow me to set a mask that the browse would only return options for the users that match the desired selection possibilities.
Title: Resolve My Query   
Name: Sai
Date: 2006-03-14 2:20:44 AM
Comment:
Hello Anand,
I'm Sai here. Really ur articles are very good. And i have a query reagarding 'FileUpload' Control. First, by clicking the Browse button, one can select the file. After that,
How to display the selected file (with entire path) into any other conrol?? like textbox or label.
Expecting your response

Regards
sai
Title: author   
Name: anand
Date: 2006-03-01 7:03:47 AM
Comment:
You can go through the article below

http://www.odetocode.com/Articles/163.aspx

You will find tons of articles to save an image to a database if you search google.
Title: Comments   
Name: Shafaqat Ali
Date: 2006-03-01 6:58:28 AM
Comment:
Hi Anand,
Good work, how do i save the same image in sql server and laterz retrieve it.
Title: thanx   
Name: aghaffar
Date: 2006-02-28 12:48:29 AM
Comment:
thanx dear your artical is reall good as for as i am concerned about . dear i am aghaffar from pakistan i am doing BSSE and so far i have worked only with C,C++ and now with c# but web is really new feild for me wana work on web now through c# can help me by ur sugetions i really am at the very beggining.
thanx .bye
Title: Answer to your doubt   
Name: Anand Narayanaswamy
Date: 2006-02-21 8:58:31 AM
Comment:
i find a word called ->FileUpload1 in this program...so i want to know, whether its a components name or any button controls name... can any one tell me

>>> It is the ID of FileUpload control. If you double click any contro from th Toolbox it will be named as Button1, TextBox1. You can chnage this if required.

I hope I've resolved your question.
Title: help   
Name: Reger
Date: 2006-02-21 4:43:38 AM
Comment:
while running the program i got this errow msg
-->"The server rejected one or more recipient addresses. The server response was: 550 5.3.2 Expired or above limit"
So can any tell me wats the problem .... and wat 'll be solution
Title: one query   
Name: Ram
Date: 2006-02-20 11:28:06 PM
Comment:
i find a word called ->FileUpload1 in this program...so i want to know, whether its a components name or any button controls name... can any one tell me
thanx in adv
Ram
Title: Very good article   
Name: Nur turk
Date: 2006-02-19 11:30:16 AM
Comment:
I really find an answer to my question. Do you have similer article or tutorial that handles how to upload images to access database useing visual web developer. my e-mail address is turkmen4@yahoo.com. Thanks so much
Title: Nice Program   
Name: sandeep
Date: 2006-02-16 5:12:20 AM
Comment:
Hi anand. My name is sandeep and im a novice in asp.net and
c#. Do u mind if i ask u certain things in asp.net? any ways nice article. have a nice time. my e-mail id is sgsandeep@gmail.com plz do send me ur e-mail id.so that i can ask u certain things over period of time. have a nice time bye.
Title: About this Article   
Name: AAP
Date: 2006-02-08 4:54:59 PM
Comment:
I think even if this code was copied, it really doesn’t matter. What matters is you have this code handy, rather than going through the books.
In Short, if you want it - use it.
Or else search for what you want.
Title: About this article   
Name: Anand
Date: 2006-02-03 2:49:00 AM
Comment:
One of the reader had commented here that I've copied this article from books. That is not true and is absolutely baseless. The above snippet is the standard code which is used for uploading a file using ASP.NET. You will most likely find the similar type of code everywhere but it does not mean that I've copied it from there. Moreover, I've provided the code in both C# and VB.NET.

- Anand

Product Spotlight
Product Spotlight 





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


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