【问题标题】:Cannot Download file in UpdatePanel无法在 UpdatePanel 中下载文件
【发布时间】:2014-02-27 21:40:29
【问题描述】:

下面的代码可以让我下载 Word 文档.....

   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

但是一旦我添加了一个 UpdatePanel - 它不会下载文件并且不会产生错误?阅读后,我添加了一个触发器,并将 ControlID 值设置为开始创建 Word doc 文件的按钮。我尝试了几种代码组合,但似乎没有任何效果。有关如何缩小范围的任何帮助?我也调试过,没有错误显示。我检查了我的下载文件夹-那里什么都没有,尝试设置无缓存(Response.Cache.SetCacheability(HttpCacheability.NoCache)),但没有奏效。一旦我删除了 UpdatePanel,那么一切似乎都正常了吗?

   <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>
        ..

完全迷失了这一点。谁能提出解决方法或如何解决这个问题?

【问题讨论】:

  • 可以在较新的浏览器中使用 XML HTTP 下载文件(请参阅 here 和 here),但我想 UpdatePanel 的代码(服务器和/或客户端)根本不支持它。
  • 也许不是,但我本以为有人可能对此有答案,因为我的假设是其他人会在我之前尝试过这个 ;-)
  • 这个变通办法有帮助吗? encosia.com/ajax-file-downloads-and-iframes
  • 我意识到这是旧的,但我也遇到了同样的问题。错误在浏览器控制台中给出

标签: asp.net .net vb.net http updatepanel


【解决方案1】:

接受的答案是完全错误的。您需要向脚本管理器注册控件。我的在母版页中,这是我用来注册任何按钮以进行正确回发的代码。

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);
                }
            }
        }

【讨论】:

  • 我接受的答案是因为我不知道正确答案是什么——否则我不会问;)。感谢您添加您的答案
  • np,我只是不想让其他人走错方向。 :-)
  • 我不确定您是如何获得母版页的,因为 Master 对我来说不是一个可访问的属性。但我刚刚做了ScriptManager.GetCurrent(Page).RegisterPostBackControl(YourButton);,它对我来说非常有效
  • @AzNjoE - 它是 System.Web.UI.Page 的一部分 --- 每个网页都有它。公共 System.Web.UI.MasterPage Master { 获取; } System.Web.UI.Page 的成员
【解决方案2】:

我的工作方式如下:

在我的更新面板中,我配置了可能会强制执行完整回发以使下载正常运行的控件。

(我也在使用母版页,这与史蒂夫的解决方案相同,但在 aspx 中注册它而不是在后面的代码中)

<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>

【讨论】:

  • 最佳答案。更新面板包裹“下载区”
【解决方案3】:

我不得不在另一个窗口中打开一个 ashx 通用处理程序并向它传递一些会话变量,例如文件名、完整路径等。

【讨论】:

    【解决方案4】:

    我是这样做的

    <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
            }
        }
    

    【讨论】:

      【解决方案5】:

      UpdatePanel 不支持文件上传或下载。有大量支持 ajax 的组件可以做到这一点,Google 是你的朋友。

      编辑:-

      一些例子:-

      http://forums.asp.net/t/1076322.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview - 我喜欢这种方法,他使用 JavaScript 注入一个 IFrame,指向负责下载文件的页面。在 UpdatePanel 中工作

      http://encosia.com/ajax-file-downloads-and-iframes/ - 类似的方法

      【讨论】:

      • 谢谢....我不完全确定我应该搜索什么?我正在使用 ASP .Net Ajax 控件,但似乎没有针对我所追求的目标,除非我错过了什么?
      • 这是不正确的,不应该是公认的答案。
      猜你喜欢
      • 2021-11-13
      • 2017-07-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 2020-04-15
      • 2021-11-27
      • 1970-01-01
      相关资源
      最近更新 更多