Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a button in ajax updatepanel, when the button is clicked a file should be downloaded, but i was not able to download it.

earlier when the buuton is not in updatepanel, file download is working fine.

can any one help in this issue.

XML
<th   height="21" align="left" valign="middle"                    
        scope="col" colspan="5">
<asp:UpdatePanel ID="UpdatePanel9" runat="server"  UpdateMode="Conditional" >
<ContentTemplate>
<asp:Button ID="Download" runat="server" Text="Btn Download " Width="142px"
CssClass="button" onclick="Download_Click" Visible="true" Enabled="false"/>
</ContentTemplate>
<Triggers>
                                                            <asp:AsyncPostBackTrigger ControlID="AType" EventName="SelectedIndexChanged" />
                                                            <asp:AsyncPostBackTrigger ControlID="tvMatched" EventName="SelectedNodeChanged" />

                                                            <asp:AsyncPostBackTrigger ControlID="SearchBtn" EventName="Click" />
                                                          </Triggers>
                                                        </asp:UpdatePanel>
                                  </th>
Posted
Updated 4-May-11 20:17pm
v2
Comments
Sunasara Imdadhusen 5-May-11 2:18am    
Added PRE tag properly!

No need IFrame you can Register script in you page and use window.open for open new webform with out ajax as contain you dowload file logic on page load event.

=====page from main script=======
output = @"C:\\Myfile.csv"
ScriptManager.RegisterStartupScript(Page, GetType(), "MyScript", "window.open(' FileDownloader.aspx?ifile=" + HttpUtility.UrlEncode(output) + "');", true);
=========New page web form for loadload file===========
C#
protected void Page_Load(object sender, EventArgs e)
        {

           string output = Page.Request.QueryString.Get("ifile");
           string iUrl = HttpUtility.UrlDecode(output);
            string Filesname = "Test Export.xlsx";
            FileInfo file = new FileInfo(iUrl);
            if (file.Exists)
            {
                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.AddHeader("content-disposition", "attachment; filename=" + Filesname);
                Response.AddHeader("Content-Type", "application/Excel");
                Response.ContentType = "application/vnd.xls";
                Response.AddHeader("Content-Length", file.Length.ToString());
                Response.WriteFile(file.FullName);
                Response.End();
            }
            else
            {
                Response.Write("This file does not exist.");

            }
 
Share this answer
 
Comments
Member 1526027 28-Apr-16 21:47pm    
This solution failed because Popup's Blockers... Does any body know how to avoid it ??
Hi
Please use following code:

XML
<Triggers>
<asp:PostBackTrigger ControlID="Download_Click">
</Triggers>


Thanks,
Imdadhusen
 
Share this answer
 
v2
Comments
camixx 10-Jun-14 10:54am    
The ControlID you're setting is incorrect according to the code above. You need to do:

<Triggers>
<asp:PostBackTrigger ControlID="Download">
</Triggers>
On the click event of the "Download_Click", open another page with an IFrame, and write your download logic on that page. Than it will work perfectly.

Because due to UpdatePanel your page doesnot refresh so downlaod will not work
 
Share this answer
 
Comments
Sandeep Mewara 5-May-11 2:28am    
Because due to UpdatePanel your page doesnot refresh so downlaod will not work
That's incorrect.

We have Async downloads now and a postback trigger would do.
solution 4 is awsome
please try it
 
Share this answer
 
Comments
CHill60 29-May-14 8:41am    
Not a solution. If you have a comment to make use the "Have a Question or Comment?" link. If you think a solution is good then click on the star rating to the top right of the appropriate post

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900