I have web API that provides stores files as a stream of bytes. The response is already fetched and saved in the state but now I want to download the file from my react application onClick of a button. I am doing it as follow:
downloadContract( binaryData ) {
const file = new Blob([binaryData], { type: 'application/pdf' });
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
}
The stream is being fetched correctly after debugging but downloading the file produces an error: Error loading PDF document.
Update:
New endpoint calling using this source:
callLoadContract: {
remote( state, id, contractId ) {
const url = `${base}/vendor/${id}/${contractId }`;
return $http.instance.api.get( url, id, contractId);
},
success: Actions.contractLoaded,
error: Actions.fail
}
Handling the response:
loadContract({id, contractId}) {
this.getInstance().callLoadContract( id, contractId );
}
contractLoaded( response ) {
if (response && response.data) {
console.log(response);
const file = new Blob([response.data], { type: 'application/pdf' });
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
}
}
Same error.
/api/download?fileId=XXXX
? – Reproach