Unable to render excel download from aspx page

旧城冷巷雨未停 提交于 2019-12-12 02:34:07

问题


I have an ASP.NET (webforms) page that renders MS-Excel back to the response stream on click of a button. 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. 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:

        // 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:

This Stackoverflow Q/A states that the meaning of the forbidden icon is that the client is terminating/aborting the response


回答1:


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

These headers should be removed and replaced with:

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


来源:https://stackoverflow.com/questions/12300154/unable-to-render-excel-download-from-aspx-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!