简体   繁体   English

无法呈现从ASPX页面下载的Excel

[英]Unable to render excel download from aspx page

I have an ASP.NET (webforms) page that renders MS-Excel back to the response stream on click of a button. 我有一个ASP.NET(网络表单)页面,单击按钮即可将MS-Excel呈现回响应流。 Everything works perfectly in testing but on live deployment, I get this dialogue box after the browser appears to be trying to download the file: 一切都可以在测试中正常运行,但是在实时部署中,浏览器似乎正在尝试下载文件后,出现了这个对话框: 在此处输入图片说明 where ReportsShow.aspx is the name of the aspx page that generates and renders the excel. 其中ReportsShow.aspx是生成并呈现Excel的aspx页面的名称。 The button that triggers the download fires a postback so I am confounded as to why the page would not be found when it renders correctly on load? 触发下载的按钮会触发回发,因此我迷惑为什么为什么在正确加载时找不到页面? I am clueless and any help would be greatly appreciated 我一无所知,任何帮助将不胜感激

EDIT : As requested by Mayank, here's the code structure: 编辑 :根据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);
            }
        }

MORE INFO 更多信息

I've analyzed this with fiddler and this is what I see for the particular request/response which is expected to present the excel for download: 我已经用提琴手对此进行了分析,这是我对特定请求/响应所看到的,该请求/响应有望提供可下载的excel:

在此处输入图片说明

This Stackoverflow Q/A states that the meaning of the forbidden icon is that the client is terminating/aborting the response 此Stackoverflow Q / A指出,禁止图标的含义是客户端正在终止/中止响应

I have found the solution to this issue. 我已经找到解决此问题的方法。 The details are as described in this link 详细信息如该链接中所述

This SO question has some details and resources that would clarify what was happening. 这样的问题有一些细节和资源可以澄清正在发生的事情。 The basic issue is that IE versions 8 and below fail to download files over SSL when they see the following headers in the response: Cache-control: no-cache Pragma: no-cache 基本问题是,当IE 8和更低版本在响应中看到以下标头时,无法通过SSL下载文件:高速缓存控制:no-cache语法:no-cache

These headers should be removed and replaced with: 这些标头应删除并替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM