I am currently facing a challenge in retrieving a PDF file from the server, which is enclosed within a JSON object.
When I send a byte array of the PDF to the front-end, I can successfully read it by configuring the responseType
to arraybuffer
. Subsequently, I can proceed to download the PDF as follows:
var blob = new Blob([data], { type: application/pdf});
if ($window.navigator && $window.navigator.msSaveOrOpenBlob) {
$window.navigator.msSaveOrOpenBlob(blob);
} else {
var a = document.createElement("a");
document.body.appendChild(a);
var fileURL = URL.createObjectURL(blob);
a.href = fileURL;
a.download = fileName;
a.click();
}
}
However, when the server sends JSON with the byte array embedded within, setting the responseType
to JSON
poses a challenge in converting the blob. So, if I set the responseType
to arrayBuffer
, I receive an array of arrayBuffers. How can I convert this into JSON while still being able to extract the PDF afterwards?
The JSON structure that I receive is as follows:
{
result: true,
value: <the pdf byte array>,
errorMessage: null
}