File Upload in UpdatePanel, ASP.NET AJAX

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?

Your comment:



 (will not be displayed)


 
 
 
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678