8

The below code works which allows me to download a Word document.....

   Try
        Response.BufferOutput = True
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.Charset = ""
        HttpContext.Current.Response.ContentType = "application/msword"
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=myfile.doc")
        HttpContext.Current.Response.Write(s)
        'HttpContext.Current.Response.End()
        HttpContext.Current.ApplicationInstance.CompleteRequest()
        HttpContext.Current.Response.Flush()
    Catch ex As Exception
        Response.Write(ex.Message)
    End Try

But as soon as i add an UpdatePanel - it doesnt download the file and no errors are generated? After reading around i added a trigger with the ControlID value set to the button that starts creating the Word doc file. I've tried several combinations of code but nothing seems to work. Any help on how to narrow this down? I've also debugged and no errors show. Ive checked my downloads folder - nothing there, tried setting no cache (Response.Cache.SetCacheability(HttpCacheability.NoCache)) and that didnt work. As soon as i remove the UpdatePanel then all seems to work?

   <asp:UpdateProgress ID="ProgressUpdate" AssociatedUpdatePanelID="UpdatePanel1" runat="server">
        <ProgressTemplate>
            <img alt="progress" src="../images/loading.gif" />
        </ProgressTemplate>
    </asp:UpdateProgress>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
     <Triggers>
            <asp:PostBackTrigger ControlID="buttonDownloadFile" />
     </Triggers>
       <ContentTemplate>
        ..

Completely lost on this one. Could anyone suggest a workaround or how to tackle this problem?

4
  • It's possible to download a file with XML HTTP in newer browsers (see here and here), but I imagine the UpdatePanel`s code (server and/or client) simply doesn't support it.
    – Tim M.
    Commented Feb 27, 2014 at 21:47
  • Perhaps not but i would have thought someone may have the answer on this as my assumption is someone else would have attempted this before i did ;-)
    – Computer
    Commented Feb 27, 2014 at 21:54
  • Does this workaround help? encosia.com/ajax-file-downloads-and-iframes
    – Tim M.
    Commented Feb 27, 2014 at 22:06
  • I realize that this is old, but I just had this same problem. The error is given in the Browser Console
    – bobjoe
    Commented Nov 13, 2020 at 21:50

5 Answers 5

17

The accepted answer is just plain wrong. You need to register the control with the scriptmanager. Mine is in the master page and here is the code i use to register any button for proper post backs.

private void MasterPageRegisterButtonForPostBack(Control bt)
        {
            MasterPage masterPage = Master;
            if (masterPage is MainMaster)
            {
                var mainMaster = masterPage as MainMaster;
                if (bt != null && mainMaster.MasterScriptManager != null)
                {
                    mainMaster.MasterScriptManager.RegisterPostBackControl(bt);
                }
            }
        }
4
  • The answer i accepted was because i didnt know what the correct answer was - otherwise i wouldnt have asked ;). Thanks for adding your answer though
    – Computer
    Commented Apr 8, 2015 at 17:17
  • 4
    np, I just didn't want that steering anyone else in the wrong direction. :-) Commented Apr 28, 2015 at 18:44
  • 7
    I wasn't sure on how you got your Master page, as Master isn't a accessible property for me. But I just did ScriptManager.GetCurrent(Page).RegisterPostBackControl(YourButton); and it worked perfectly for me
    – AzNjoE
    Commented Nov 2, 2016 at 21:10
  • @AzNjoE - It is part of theSystem.Web.UI.Page --- Every web page has it. public System.Web.UI.MasterPage Master { get; } Member of System.Web.UI.Page Commented Nov 3, 2016 at 15:30
11

I got it working the following way:

inside my Update Panel I configured the controls that may forece a full postback in order to get the download working.

(I'm using also Master pages,this is the same solution as Steve's but registering it in the aspx and not in code behind)

<asp:UpdatePanel runat="server" ID="UpdatePanelDownload" UpdateMode="Conditional" ChildrenAsTriggers="True">
  <ContentTemplate>
      <asp:LinkButton ID="LinkButtonDownload" OnClick="Download_Click" runat="Server">Save XML</asp:LinkButton>
 </ContentTemplate>
  <Triggers>
    <asp:PostBackTrigger ControlID="LinkButtonDownlod" />
  </Triggers>
</asp:UpdatePanel>
1
  • Best answer. Update panel wrap up the "download area" Commented Apr 16, 2021 at 18:42
0

I had to open an ashx Generic Handler in another window and passed some session variables to it, like filename, full path, etc.

0

I did it like this

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <%foreach(var f in listOfObject){ %>
      <a class="btn btn-danger" href="javascript:__doPostBack('ctl00$BodyContent$butDownload', '<%=f.ID %>')" >Download</a>
    <%} %>
    </ContentTemplate>
</asp:UpdatePanel>  
    
<asp:LinkButton runat="server" ID="butDownload" OnClick="butDownload_Click" style="display:none;">LinkButton</asp:LinkButton>


///----Server-Side-----
    protected void butDownload_Click(object sender, EventArgs e)
    {
        string strID = Request["__EVENTARGUMENT"];
        if (!string.IsNullOrEmpty(strID))
        {
            int id = int.Parse(strID);
            //---Do what you want here
        }
    }
-6

The UpdatePanel does not support file upload or download. There are tons of ajax-enabled components out there that will do this, Google is your friend.

EDIT: -

Some examples: -

http://forums.asp.net/t/1076322.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview - I like this approach, he injects an IFrame using JavaScript that points to a page responsible for downloading the file. Works inside an UpdatePanel

http://encosia.com/ajax-file-downloads-and-iframes/ - Similar approach

2
  • Thanks.... Im not entirely sure what im supposed to be searching for? Im using ASP .Net Ajax controls but non seem to be targeting towards what im after unless ive missed something out?
    – Computer
    Commented Feb 28, 2014 at 14:23
  • 2
    This is incorrect and should not be the accepted answer.
    – Stan Shaw
    Commented Dec 4, 2015 at 14:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.