Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 无法在UpdatePanel中下载文件_Asp.net_.net_Vb.net_Http_Updatepanel - Fatal编程技术网

Asp.net 无法在UpdatePanel中下载文件

Asp.net 无法在UpdatePanel中下载文件,asp.net,.net,vb.net,http,updatepanel,Asp.net,.net,Vb.net,Http,Updatepanel,下面的代码可以让我下载Word文档 Try Response.BufferOutput = True HttpContext.Current.Response.Clear() HttpContext.Current.Response.Charset = "" HttpContext.Current.Response.ContentType = "application/msword" HttpContext.C

下面的代码可以让我下载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,它就不会下载文件,也不会生成错误?在阅读了一遍之后,我在开始创建Word文档文件的按钮上添加了一个设置了ControlID值的触发器。我尝试了几种代码组合,但似乎没有任何效果。有没有关于如何缩小范围的帮助?我也调试过,没有错误显示。我检查了我的下载文件夹-那里什么都没有,尝试设置没有缓存(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>
        ..

..

完全迷失在这一点上。是否有人可以建议解决方法或如何解决此问题?

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

编辑:-

一些例子:-

-我喜欢这种方法,他使用JavaScript注入IFrame,指向负责下载文件的页面。在UpdatePanel中工作


-类似的方法

公认的答案完全是错误的。您需要向scriptmanager注册该控件。我的在母版页中,这是我用来注册任何按钮的代码,用于正确的回发

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

我通过以下方式使其工作:

在我的更新面板中,我配置了可以阻止完全回发的控件,以使下载工作正常

(我也使用母版页,这与Steve的解决方案相同,但在aspx中注册,而不是在代码隐藏中注册)


保存XML

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

我就是这样做的

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

链接按钮
///----服务器端-----
受保护的void butDownload\u单击(对象发送者,事件参数e)
{
string strID=Request[“\u EVENTARGUMENT”];
如果(!string.IsNullOrEmpty(strID))
{
int id=int.Parse(strID);
//---你想在这里干什么就干什么
}
}

可以在较新的浏览器中下载带有XML HTTP的文件(请参见和),但我认为UpdatePanel的代码(服务器和/或客户端)根本不支持它。也许不支持,但我认为可能有人对此有答案,因为我的假设是其他人在我之前会尝试;-)这个变通方法有用吗?我意识到这很古老,但我也有同样的问题。该错误在浏览器控制台中给出。。。。我不完全确定我应该搜索什么?我使用ASP.Net Ajax控件,但似乎不是针对我想要的东西,除非我遗漏了什么?这是不正确的,不应该是公认的答案。我接受的答案是因为我不知道正确的答案是什么——否则我不会问;)。谢谢你添加你的答案,我只是不想让别人误入歧途。:-)我不确定您是如何获得母版页的,因为
Master
不是我可以访问的属性。但我只是做了
ScriptManager.GetCurrent(Page.RegisterPostBackControl(YourButton))并且它对您的应用非常有效me@AzNjoE-它是system.Web.UI.Page的一部分---每个网页都有它。public System.Web.UI.MasterPage母版{get;}System.Web.UI.PageBest answer的成员。更新面板包装“下载区域”
<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
        }
    }