【问题标题】:Unable to render excel download from aspx page无法从 aspx 页面呈现 excel 下载
【发布时间】:2012-09-06 12:28:40
【问题描述】:

我有一个 ASP.NET (webforms) 页面,通过单击按钮将 MS-Excel 呈现回响应流。一切都在测试中完美运行,但在实时部署中,在浏览器似乎正在尝试下载文件后,我得到了这个对话框: 其中 ReportsShow.aspx 是生成和呈现 excel 的 aspx 页面的名称。 触发下载的按钮会触发回发,所以我很困惑为什么页面在加载时正确呈现时找不到? 我一无所知,任何帮助将不胜感激

编辑:按照 Mayank 的要求,代码结构如下:

        // Get instance of reporting service
        IReportingService reportingServiceClient = Reports.GetWebService();
        // Get a fresh copy of the report
        BusinessReport theReport = reportingServiceClient.GetReport(AcctList.ToArray());

        ExcelExport excelExport = new ExcelExport();

        const string templateFileName = "Business-Report.xls";
        string newFileName = String.Empty;

        try
        {
            newFileName = excelExport.CopyTemplateFile(Server.MapPath("~/ExportTemplates/" + templateFileName));
            excelExport.WriteData(forexOptionReport, newFileName);

            Response.Clear();

            Response.AddHeader("content-disposition", string.Format("Attachment; filename=\"{0}\"", "Business-Report" + ".xls"));
            Response.ContentType = "application/vnd.ms-excel";

            Response.TransmitFile(newFileName);
            Response.Flush();
        }
        catch (Exception ex)
        {
            Errors.LogException("Error in Reports.BtnDownloadToExcel_Click", ex);
            throw;
        }
        finally
        {
            if (!String.IsNullOrEmpty(newFileName))
            {
                excelExport.DeleteFile(newFileName);
            }
        }

更多信息

我已经用提琴手分析了这个,这就是我看到的特定请求/响应,预计会呈现 excel 以供下载:

This Stackoverflow Q/A表示禁止图标的含义是客户端正在终止/中止响应

【问题讨论】:

  • 您能分享您的代码吗...以便我们查看您为此做了什么...

标签: c# asp.net internet-explorer iis http-headers


【解决方案1】:

我已经找到了解决这个问题的方法。详情见this link

This SO question 有一些细节和资源可以澄清正在发生的事情。 基本问题是 IE 8 及以下版本在响应中看到以下标头时无法通过 SSL 下载文件: 缓存控制:无缓存 Pragma: 无缓存

这些标题应该被删除并替换为:

Response.Headers.Set("Cache-Control", "private, max-age=0");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多