File Upload in UpdatePanel, ASP.NET AJAX

UDPATE: October 2, 2009

There is a better way of doing it with Ajax Control Toolkit version 3.0.30930 which works with .NET 3.5 SP1 and Visual Studio 2008 SP1.  Please read this post for a step by step instruction

One of the common queries I get across my sessions is that, the File Upload control doesnt work inside an Update panel.  All of us would like to implement a Gmail File Upload kind of interface and when you try to implement a similar thing using UpdatePanel (which works like a charm for other activities), it simply doesn't work.

The behaviour is expected.  The File Upload Control doesnt work inside an Update Panel due to security reasons and restrictions a browser implies.  They dont allow Javascript files to directly access the files in an user's sytem and dont allow to modify or access the details of a file when working with the File Upload Control.

There are a couple of ways to solve this issue, one using Update Panel and Post Back Triggers and the other using Iframes.

1. Use Update Panel, File Upload Control and use a PostBackTrigger Control to force a postback only for the File Upload Control

This approach works well without much tweaking except for that, there would be a postback only for the File Upload Control.  While the rest of the stuff happens asynchronously, using the UpdatePanel, when the user presses the "Upload" Button, the page will be refreshed.  Let us examine how we can accomplish this.  Place the following code within the <form> </form> tags in your ASP.NET Page.  

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

 <br />

<asp:Button ID="Button1" runat="server" Height="20px" onclick="Button1_Click" Text="Submit" Width="128px" />

<br />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<br />

<asp:FileUpload ID="FileUpload1" runat="server" />

<br />

<asp:Button ID="Button2" runat="server" Height="25px" onclick="Button2_Click" Text="Upload" Width="128px" />

<br />

<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

<br />

</ContentTemplate>

<Triggers>

<asp:PostBackTrigger ControlID="Button2" />

</Triggers>

</asp:UpdatePanel>

In the Code behind, add the following lines of code:-

 

protected void Button1_Click(object sender, EventArgs e)

{

Label1.Text = TextBox1.Text;

}

protected void Button2_Click(object sender, EventArgs e)

{

FileUpload1.PostedFile.SaveAs(@"C:\test\"+System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName));

Label2.Text = FileUpload1.PostedFile.FileName;

}

If you run the above sample, you would notice that upon entering something in the TextBox and clicking "Submit" (Button1) the Label above the File Upload Content, shows the Text you typed, without a page refresh.

However, when you select a file and click on the "Upload" (Button2) Button, you would notice that a postback happens and the file gets posted to the "C:\Test\" folder and also the full path is specified in the Label 2.

In the above code, I have not taken any steps regarding validation, checking if file exists etc., since it just shows how you could accomplish File Upload within Update panel.  In normal cases, you would write better code to accomplish a file upload feature.

2. Use Iframes and accomplish a truly Gmail Like File Upload Interface.

I thought of writing a post on this, but did a quick research and found that there are a few solutions posted by our MVPs / Community Folks and just thought of providing a link to the same.

http://vinayakshrestha.wordpress.com/2007/03/13/uploading-files-using-aspnet-ajax-extensions/

http://msmvps.com/blogs/luisabreu/archive/2006/12/14/uploading-files-without-a-full-postback.aspx

Note that this post doesnt claim any warranty / support for the above articles, though.

Cheers !!!

 

posted @ Tuesday, April 01, 2008 11:03 PM

Print

Comments on this entry:

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Asim Suvedi at 4/26/2008 10:46 PM
Gravatar
I think you are doing a tremendous job...ur helping the new programming generation excel and speed up. Your writing style is simple ( without bulky words for which reader has to refer to dictionary). You are one of the finest in this field. Cheers!!!

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by XeronLiu at 5/3/2008 8:54 PM
Gravatar
Great job u've done.
Thanks for ur kindly help

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Maynor at 5/15/2008 9:45 AM
Gravatar
I still have a question, i am using the devexpress gridview and in the edit form, i used the update panel to insert the fileupload control a similar example in this page

http://demos.devexpress.com/Tutorials/Grid/Editing/EditFormUploadFile/EditFormUploadFile.aspx

the problem is that when i try to access the fileupload control in the code behind it says it doesnt exist, why?

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by matt ridley at 5/21/2008 7:51 AM
Gravatar
Thanks for the post, I was having more issues where the first upload wouldn't work but the second would, after looking around some more I found this post, I had my uplaod control inside a panel that was visible=false on load, then displayed if the user wanted to upload, this link explains whats up and a solution. Thanks again.
http://marss.co.ua/FileUploadAndUpdatePanel.aspx

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Surender at 6/6/2008 7:03 PM
Gravatar
Hi i have Problem that FileUpload1.PostedFile Showing null i.e. FileUpload1.PostedFile.FileName object not set to refference Help me asap

surendersardana@gmail.com

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Gregory at 6/18/2008 8:54 PM
Gravatar
Surender,

Please check that your form element has enctype="multipart/form-data" attribute.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Narendra Kumar at 6/23/2008 11:36 PM
Gravatar
Thanks for your valuable help.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Aruna at 7/16/2008 2:54 AM
Gravatar
Great! Thanks Gregory.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by shailesh at 7/31/2008 10:08 PM
Gravatar
it is a great great artice and i really appreciate your effort for this.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Mortaza Doulaty at 9/4/2008 11:44 PM
Gravatar
Thanks for your help...

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Download games at 9/8/2008 11:37 AM
Gravatar
thanks for everyones help with this

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Jeremy Libertor at 9/24/2008 2:24 PM
Gravatar
What can I say other than "Thank you it was a perfect solution!"

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Vikash yadav at 12/2/2008 3:48 PM
Gravatar
just go through the following thread.
http://www.muraton.net/post/2008/01/FileUpload-
and-Ajax-UpdatePanel.aspx

it may help

cheers
Vikash yadav

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Graham at 12/2/2008 9:32 PM
Gravatar
Absolutely spot on. With the above mentioned change to the form control, the perfect solution. Thanks!

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Dennis Wong at 12/11/2008 5:49 PM
Gravatar
Hi, this is a great article.
However, I've got some problems which I can't really solve..

Here's my structure of my files:
1. MasterPage (*.master)
2. -- ChildPage (*.aspx)
3. -- CustomControl (*.ascx)

My "Uploadfile" control is in the CustomControl.ascx page.

My master page holds the frames for my site.
So the childPage is in one of the table cells within it, with a ContentPlaceHolder.

How do I implement your FileUpload control within my customControl.ascx page?
How do I call the button.click to refresh the main page hosting the iframe?

Thanks!

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Ratul at 12/14/2008 1:31 AM
Gravatar
Thankx... Its working...Damn good...
'm keen to know the details "why FileUpload is making problem with UpdatePanel?"

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Jack at 1/7/2009 11:21 AM
Gravatar
Our company use the ajaxuploader and now this problem has gone. cheers.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Hn at 1/9/2009 1:55 AM
Gravatar
How do I specify the upload control if it is in a TabContainer? I would get this error A control with ID 'btnUpload' could not be found for the trigger in UpdatePanel 'UpdatePanel1'. Thanks in advance.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by çeviri at 1/9/2009 8:27 PM
Gravatar
Thanks for the post, I was having more issues where the first upload wouldn't work but the second would, after looking around some more I found this post, I had my uplaod control inside a panel that was visible=false on load, then displayed if the user wanted to upload, this link explains whats up and a solution. Thanks again..
Çeviri dil hizmetleri ingilizce rusça çeviri

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Savada Sin at 1/10/2009 2:44 PM
Gravatar
That is so good,

Thanks,

Savada

# FileUpload.FileName Property displays blank

Left by Anit B Patel at 2/20/2009 1:05 PM
Gravatar
I have used fileupload control in update panel but after select a file when i am clicking on asp button at that time FileUpload.FileName properties are displayed blank other side i have selected file throgh fileupload control

# File Upload in DevExpress first time file not found

Left by nikhil at 3/4/2009 5:28 PM
Gravatar
i am using the devexpress gridview and in the edit form
the problem is that when i try to access the fileupload control in the code behind it says it doesnt exist first time , why?

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Pawankoti at 3/6/2009 9:34 PM
Gravatar
I am using asp.net 2.0 Fileupload control in Updatepanel

i want to store the filename into database ,but it is showing sometimes object reference not set to an instance of an object ,

I want to store the filename into database?

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by dll at 3/14/2009 8:08 PM
Gravatar
I had my uplaod control inside a panel that was visible=false on load, then displayed if the user wanted to upload, this link explains whats up and a solution.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Jessica at 3/16/2009 11:50 PM
Gravatar
Thank you so much! this worked for me, although my file upload was inside a TabPanel so I had to write the entire path for the control.

http://forums.asp.net/p/1119192/1759621.aspx

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Vani at 3/20/2009 11:10 AM
Gravatar
i am not able to save my file when i click my button for the first time. but from second click onwards the file is being saved.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Uday Kumar Lazurus at 4/8/2009 9:20 PM
Gravatar
I am using dotnetnuke module development framework where
Here's my structure of my files:

1. -- MainPage (*.aspx)
2. -- CustomControl (*.ascx)

My "Uploadfile" control is in the CustomControl.ascx page.

CustomControl Page has ContentPlaceHolder.

How do I implement our FileUpload control within my customControl.ascx page?
How do I call the button.click to refresh the mainpage? Any work around thought is welcome.. Please urgent project deadline is on the head.. the MainPage.aspx is not under our full control it is generated dnn(dotnetnuke framework--setting.ascx page in the dnn module for further refernce)

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Nayan at 4/16/2009 4:33 PM
Gravatar
What the meaning if we use postback Trigger the whole page is postback to server so whats the benefit i think this is worst case we have to use javascript

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by vandna at 5/19/2009 2:19 PM
Gravatar
thanks it was gr8 help

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Paul at 5/28/2009 8:29 AM
Gravatar
this was a great help thank you so much, from Philippines

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Paul at 5/28/2009 9:01 AM
Gravatar
Hello again i have a problem at first try it was not able to upload the file but in the succeeding attempt it was ok, i don't know what the problem is can you please help me?

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Siva at 7/23/2009 10:51 AM
Gravatar
This is a wonderful article. It helped me get through my issues pretty quick. Once again thanks for your support.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by suresh at 7/29/2009 1:46 AM
Gravatar
It is very nice... I also need to show the update progress within this.. but when i do that the content in the updateprogress is not showing..

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Munish Singla at 7/31/2009 11:37 AM
Gravatar
Thanks for this. It really help & Good. :-)

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Icebob at 8/4/2009 2:13 AM
Gravatar
Thx man ;) You saved my time ;)

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Heba at 8/6/2009 8:55 AM
Gravatar
Thank you very much...

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Kamal at 8/7/2009 7:21 AM
Gravatar
Thanks Dear, After finding lot of website.. finaly i got your code.. I used that code... It is working fine..
thanks a lot

Kamal

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Utility Warehouse at 8/12/2009 2:39 PM
Gravatar
This has just saved my a huige amount of time!

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by mohammed at 8/14/2009 8:07 AM
Gravatar
very nice !
thank u

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by dadadada ad at 8/26/2009 2:33 AM
Gravatar
aad adadadadadad adad

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by jalal at 9/1/2009 7:17 AM
Gravatar
tanx my dear!

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by pavana at 9/2/2009 4:01 AM
Gravatar
hi,

For me the situation is different from the above.
like i want to upload the file only on click of save along with the other controls.
In this case can i write triggers for my save button? the click of save button im inserting all other page control values in to database.

Please suggest me.

Thanks in advance
Pavana

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by rjobradley at 9/3/2009 2:25 PM
Gravatar
UpdatePanel trigger is not necessary. Remove that and change:
<asp:Button ID="Button2" runat="server" Height="25px" onclick="Button2_Click" Text="Upload" Width="128px" />
to this
<asp:Button ID="Button2" runat="server" Height="25px" PostBackUrl="#" Text="Upload" Width="128px" />

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by pawan at 9/4/2009 1:00 PM
Gravatar
thanks a lot..it helped me

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by dionisio at 9/15/2009 10:44 PM
Gravatar
hello,the "fileupload.hasfile" it only works on the 2nd try, it appens to anyone else ?
thanks.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Diego at 9/22/2009 1:16 AM
Gravatar
Thanks a lot, it was very gratefull...
I have another issue maybe anyone can help me.
It is the same case it was described but after you choose a file (gif) , that .gif appears in an Image Object.
As far as i was able to see, it is an input type="file" and on the onChange="" event, calls a JS function that does the rest. But I could find how to do it..(load de image when you close de Dialog window without pressing any Server Button...
Thaks a lot to everyone

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Ray at 9/23/2009 9:07 PM
Gravatar
Thanks for share!!!

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Pankaj Baranwal at 9/29/2009 11:39 PM
Gravatar
thanks, it helped a lot to me. i was thinking it is not possible but your article helped me a lot.
simply you described difficult thing.

SUPERB....
keep it up.
cheers

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Mark at 10/23/2009 1:27 AM
Gravatar
I posted a solution without the postback trigger on my blog:
http://www.dailycode.net/blog/post/asp-fileupload-in-ajax-update-panel.aspx

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by MAthieu at 10/28/2009 12:55 PM
Gravatar
NEed an example with a gridview.
mathieu_cupryk@hotmail.com

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Jalpesh at 11/5/2009 3:51 AM
Gravatar
No need to do the postback for the file upload control.

put the
Page.Form.Attributes.Add("enctype", "multipart/form-data");

and it is will work fine with update panel.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Haissam Abdul Malak at 12/7/2009 8:56 AM
Gravatar
Thanks for share!!! Just let you know the following product also works in update panel and it is very stable.

http://ajaxuploader.com

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Shreedhar at 12/17/2009 7:16 PM
Gravatar
Your one Idea has changed My Page style.. :)
Thanks a lot for your timely help.. Nice Article Dear

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by baban at 12/31/2009 11:38 PM
Gravatar
Great job u've done.
Thanks for ur kindly help

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Maddy at 1/7/2010 10:37 PM
Gravatar
Sorry i tried with the 1st option bt its not working properly in my application. SO could you please helpmein detail ?

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Rasika W. at 1/11/2010 5:30 PM
Gravatar
Thanks for the tip about PostBackTriggers!

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Henriette at 1/27/2010 7:43 PM
Gravatar
Thanks a lot. I've searched for a solution to using asp:fileupload in updatepanel. Most of my findings were placed the fileupload control outside of updatepanel.
I really liked your very simple solution. Adding the postbacktrigger did the trick!

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Afzal at 1/30/2010 6:56 PM
Gravatar
Very nice way explaination...U r generous in explaining other dependencies too.

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by Rishi Karott at 1/31/2010 1:27 AM
Gravatar
Very helpful writing!
Keep the same...

learn what U need... share what U know...!!!

# re: File Upload in UpdatePanel, ASP.NET AJAX

Left by mohit at 2/5/2010 5:57 PM
Gravatar
hi
i am extremly thank ful to you for publishing such a good article . and thanks to all who express their view in forum. those views help me in sorting out my problem.
but still i am facing one problem:
when i am using httpfilecollection with file upload to upload multiple files at one shot(within update panel) that time the adding only enctype="multipart/form-data" is not doing things to me. i a have to include postbacktrigger with button control id. is their any way besides this. thanks in advance

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213