Download PDF file, Angular 6 and Web API
I am assuming you are using .Net Core.
You return type is HttpResponseMessage. For .Net Core onward, it should be IActionResult.
So, in your case, you will be returning
return File(<filepath-or-stream>, <content-type>)
Or
You have to make one small change in your Startup.cs file:
services.AddMvc().AddWebApiConventions();
Then, I am not 100% sure here, but you have to change the routing too:
routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
import { Injectable } from "@angular/core";
declare var $;
@Injectable()
export class DownloadFileService {
save(file, fileName) {
if (window.navigator.msSaveOrOpenBlob) {
// IE specific download.
navigator.msSaveBlob(file, fileName);
} else {
const downloadLink = document.createElement("a");
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.setAttribute("href", window.URL.createObjectURL(file));
downloadLink.setAttribute("download", fileName);
downloadLink.click();
document.body.removeChild(downloadLink);
}
}
}