I am trying to download PDF from react app using fetchAPI as following :
const testApiURL = "http://localhost:5288/api/files/download/?fileUrl=location/500-KB.pdf";
function clickHandle() {
fetch(testApiURL, {
headers: { "Content-Type": "application/pdf", "responseType": "arraybuffer" }
})
.then(response => response.blob()
.then((blob) => {
const fileURL = window.URL.createObjectURL(blob);
// Setting various property values
let alink = document.createElement("a");
alink.href = fileURL;
alink.download = "MyFile.pdf";
alink.click();
})
.catch(error => console.error(error)));
}
REST API returns byteArray , but when when opening downloaded file in Downnloads folders – getting error “File corrupted”. After debugging it was found that inspite of that content type provided is “application/pdf” response got as “application/json”. What is missing / wrong in the fetch call ? Thanks in advance