bhushan bhushan - 4 months ago 59
AngularJS Question

PDF is not downloading on Chrome localhost and Safari

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**`
});
}


This is my code but it is not working for Chrome localhost and Safari browsers.

Answer

ASP.NET offers Response.TransmitFile for downloading a file. The following code will do the trick.

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

UPDATE

if you have problems with safari browser then change AppendHeader to SetHeader in the code.

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