Cannot Download file in UpdatePanel

2020-07-11 07:33发布

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条回答
beautiful°
2楼-- · 2020-07-11 08:20

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

查看更多
不美不萌又怎样
3楼-- · 2020-07-11 08:20

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

查看更多
淡お忘
4楼-- · 2020-07-11 08:25

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>
查看更多
放我归山
5楼-- · 2020-07-11 08:32

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);
                }
            }
        }
查看更多
登录 后发表回答