Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download feature not working within update panel in asp.net

Tags:

c#

ajax

asp.net

I have a Web User Control containing a FormView. The formview shows details of job seeker. I have provided a button for "Download Resume" link, so that admin/HR can download the resume. I have placed this control in an aspx page that contains the UpdatePanel. Everything works fine except Download Link.

I have given a Command on donwload link button and a function is associated with the command to start download.

Below is the code i have implemented -

//Command on 'Download' link button within FormView protected void lnkDownload_Command(object sender, CommandEventArgs e) {     if (e.CommandName.Equals("Download"))     {         StartDownload(e.CommandArgument.ToString());     } }  //My routine to download document //sFileInfo contains filepath$==$mimetype protected void StartDownload(string sFileInfo) {     string[] d = sFileInfo.ToString().Split((new string[] { "$==$" }), StringSplitOptions.None);     string filename = d[0];     string docType = d[1];      System.IO.FileInfo file = new System.IO.FileInfo(d[0]);      if (file.Exists)     {         Response.Clear();         Response.AddHeader("Content-Disposition", "attachment; filename=" + d[0]);         Response.AddHeader("Content-Length", file.Length.ToString());         Response.ContentType = d[1];         Response.WriteFile(file.FullName);         Response.End();     }     else     {         Server.Transfer("~/Mesgbox.aspx?cat=2");     } } 

The code works perfectly if update panel is removed but generates script errors if update panel is used.

Any suggestions....?

Thanks for sharing your time.

like image 523
IrfanRaza Avatar asked Mar 28 '11 15:03

IrfanRaza


2 Answers

To initiate a full page postback, you add a postback trigger to your update panel:

<asp:UpdatePanel runat="server">     <Triggers>         <asp:PostBackTrigger ControlID="YourControlID" />     </Triggers>     <ContentTemplate>         ..... 
like image 61
Mitul Avatar answered Sep 22 '22 15:09

Mitul


You cannot return an attachment in an UpdatePanel partial postback, since the results are used by the ScriptManager to update a DIV (not the whole response). The simplest fix for what you're trying to do would be to make your download button as a postback control. That would cause that button to initiate a full postback. Here's the code below to include in your Page_Load

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(this.lnkDownload); 
like image 30
Ken Melton Avatar answered Sep 24 '22 15:09

Ken Melton