I have an angular2 project with an ASP.Net Web API. I have code to retrieve a file path from my database which goes to a document on my server. I then want to display this d
Angular v.5.2.1 answer:
In *.services.ts
downloadPDF(): any {
return this._httpClient.get(url, { responseType: 'blob'})
.map(res => {
return new Blob([res], { type: 'application/pdf', });
});
}
And then in *.component.ts
downloadPDF() {
let tab = window.open();
this._getPdfService
.downloadPDF()
.subscribe(data => {
const fileUrl = URL.createObjectURL(data);
tab.location.href = fileUrl;
});
}
Its important to initiate and open a new tab before calling the service. Then set this tabs url to the fileurl.