PDF不会在Chrome localhost和Safari上下载

时间:2016-07-22 10:30:50

标签: c# asp.net angularjs

PDFHelper.cs

 byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("content-length",                     bytes.Length.ToString());
                HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
                //HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                HttpContext.Current.Response.BinaryWrite(bytes);
                HttpContext.Current.Response.End();

LoginFactory.js

        factory.getPDFReport = function (data) {
        var headers = { 'Content-Type': 'application/pdf;' };
        var url = hostUrl + '/ReportsController/PDFReports';
        return $http.post(url, data, headers).then(
           function (results) {
               return results;`**enter code here**`
           });
    }

这是我的代码,但它不适用于Chrome localhost和Safari浏览器。

1 个答案:

答案 0 :(得分:0)

ASP.NET提供Response.TransmitFile用于下载文件。以下代码将起到作用。

Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=file_name.pdf");
Response.TransmitFile(file_path);
Response.End(); 

<强>更新

如果您在使用Safari浏览器时遇到问题,请在代码中将AppendHeader更改为SetHeader

Response.SetHeader("Content-Disposition", "attachment; filename=file_name.pdf");