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浏览器。
答案 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");