UpdatePanel无法导出下载文件

转自 http://www.cnblogs.com/vipsoft/p/3298299.html

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

时间: 2024-07-29 01:48:02

UpdatePanel无法导出下载文件的相关文章

MVC FileResult 导出/下载 文件/Excel

参考http://www.cnblogs.com/ldp615/archive/2010/09/17/asp-net-mvc-file-result.html

比较恶心的360老年版浏览器 文件导出下载

function export_txt($data) { Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); $destination_folder='./Public/file/'; //txt文件 $file_name=iconv('utf-8', 'gbk', '账号卡信息').date('Y-m-d H-i-s',time()).'.txt'; $

一、【Spring Boot】 Excel文件导出下载

   Spring Boot Excel 文件导出 目标: 实现Excel文件的直接导出下载,后续开发不需要开发很多代码,直接继承已经写好的代码,增加一个Xml配置就可以直接导出. 实现: 1.抽象类 BaseExcelView 继承 webmvc 的 AbstractXlsxStreamingView 抽象类, AbstractXlsxStreamingView 是webmvc继承了最顶层View接口,是可以直接大量数据导出的不会造成内存泄漏问题,即 SXSSFWorkbook 解决了内存问题

Java POI导出Excel不弹框选择下载路径(下载文件不选择下载路径,默认) Chrome

在Chrome浏览器中,Java导出Excel文件时,浏览器弹出提示框,需要选择下载路径 在Chrome中的高级设置中,把“下载前询问每个文件的保存位置”去掉就解决了 DEEPLOVE(LC) 原文地址:https://www.cnblogs.com/ldl326308/p/10960755.html

php导出内容到txt并自动弹出下载文件

php将内容保存到txt文件中,并自动弹出下载文件窗口的方法: $id=array('我爱学习网http://www.5ixuexiwang.com','汇享在线工具箱http://tool.huixiang360.com'); header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header("Content-Disposition:attachm

asp.net中下载文件的问题

今天解决web的文件下载问题,下载的方法网上很多,不过我的下载有点特殊: 1.下载按钮在gridview中,是模板列的linkButton: 2.使用了ajax控件: 所以,在下载时总是报错,通过查找资料,解决方法如下: 1.先说ajax控件的问题: 如果下载按钮在ajax控件上,需要添加Triggers节点,如下: aspx: <asp:UpdatePanel ID="UpdatePanel3" runat="server"> <ContentT

ADF Faces导出Excel文件【附样例工程】

本文提供一个基于ADF Face组件开发样例工程,工程的实现过程分为3个部分以应对Excel导出开发中常见的处理. 1.空模版文件下载:将Excel文件视为普通文件提供下载操作. 2.数据文件输出,将数据内容输出为Excel文件,目标文件尽在服务端内存中存在,这种方式需要对Excel文件的内容处理,需要引入响应的类库. 3.模版文件填充数据后下载,基于服务端的物理文件为模板,将业务数据填入约定位置后提供下载,在实现方面需要为工作簿对象指定源文件输入流,并完成后续内容处理. 实现的基本思路,由AD

如何用 JavaScript 下载文件

简介 我们知道,下载文件是一个非常常见的需求,但由于浏览器的安全策略的限制,我们通常只能通过一个额外的页面,访问某个文件的 url 来实现下载功能,但是这种用户体验非常不好.幸好,HTML 5 里面为 <a> 标签添加了一个 download 的属性,我们可以轻易的利用它来实现下载功能,再也不需要用以前的笨办法了. 原理 我们先看看 download 的使用方法: <a href="http://somehost/somefile.zip" download=&quo

asp mvc 导出txt 文件泛型方法

asp mvc 导出txt 文件泛型方法分享: public static void ExportFile<T>(T obj) { StringBuilder str = new StringBuilder(); //列出obj 对象中的所有属性 System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties(); if (properties != null && properties.Len