In my web application, I have a few links that lead to files with different content types - some are application/pdf
and others are image/jpeg
. When clicking on these links, the file should download or save based on their respective type.
While downloading PDFs works perfectly with the code below, I am facing an issue when trying to download images from a URL.
I have attempted to change the Content-Type
and response type to image/jpeg
, but unfortunately, it is not working as expected.
downloadDocFile(fileLocation, fileName) {
var fileNAme = fileName;
var url = fileLocation;
let headerD = this.service.getHeaderDict();
const headerDict = {
'Content-Type': 'application/pdf',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE',
'Access-Control-Allow-Headers': 'Authorization, X-Requested-With, Content-Type, Origin, Accept, X-clientid, X-locale, X-loggedin, X-version',
'Access-Control-Allow-Credentials': true
}
const requestOptions = {
headers: new Headers(headerDict), responseType: ResponseContentType.Blob
};
const proxyurl = "https://cors-anywhere.herokuapp.com/";
this.http.get(proxyurl +url,requestOptions).subscribe(
res => {
const data: Blob = new Blob([res.blob()], { type: 'application/pdf' });
saveAs(data, fileNAme);
})}
service.ts
getHeaderDict(): Object {
return this.headerDict
}