UpdatePanel无法导出下载文件

 

复制代码
protected void Page_Load(object sender, EventArgs e)
{ 
    ScriptManager sm = Page.Master.FindControl("ScriptManager1") as ScriptManager;
    if (sm != null)
    {
        foreach (GridViewRow item in gvInvoiceHistory.Rows)
        {
            LinkButton lbtnTemp = item.FindControl("btnInvoiceNumber") as LinkButton;
            if (lbtnTemp != null)
            {
                sm.RegisterPostBackControl(lbtnTemp);
            }
        }
    }  
}

protected void btnInvoiceNumber_OnClick(object sender, EventArgs e)
{
    LinkButton btnTemp = sender as LinkButton;
    if (btnTemp != null)
    {
        var path = ConfigurationManager.AppSettings["InvoicePdfPath"];
        var fileName = btnTemp.Text.Trim() + ".pdf";
        if (!FileHandler.DownLoadFile(path, fileName))
        {
            var msg = GetLocalResourceObject("InvoiceIsNotAvailable").ToString();
            ScriptManager.RegisterStartupScript(upMain, upMain.Page.GetType(), "DownLoadFile", string.Format("alert('{0}');", msg), true);
 
            //如果不存在就跳转,参见 http://www.cnblogs.com/vipsoft/p/3627683.html
      //string url = "http://www.vipsoft.com.cn";
      //ResponseRedirect.Redirect(Response, url, "_blank", "'toolbar=0,scrollbars=1,status=0,menubar=0,resizable=1,top=0,left=0,height=800,width=1000");}



        }
    }
}

//Updatepanel 会有异常 参考下面的链接查看下载方法
public static bool DownLoadFile(string path,string fileName)
{
    bool result = false;
    string filePath = HttpContext.Current.Server.MapPath(path + fileName);
    if (File.Exists(filePath))
    {
        FileInfo fileInfo = new FileInfo(filePath);
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
        HttpContext.Current.Response.ContentType = "application/octet-stream";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        HttpContext.Current.Response.WriteFile(fileInfo.FullName);
        HttpContext.Current.Response.Flush();
        HttpContext.Current.Response.End();
        result = true;
    }
    return result;
}
复制代码

 

ScriptManager1 放在 MasterPage中,查找 ScriptManager1 时,
Page.Master.FindControl("ScriptManager1") as ScriptManager;

 

FileHandler文件下载:http://www.cnblogs.com/vipsoft/p/3521958.html

如果不存在就跳转,参见 http://www.cnblogs.com/vipsoft/p/3627683.html

 

posted @   VipSoft  阅读(1994)  评论(0编辑  收藏  举报
编辑推荐:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2012-09-03 ORA-28000: the account is locked-的解决办法
点击右上角即可分享
微信分享提示