I am attempting to convert a byte array received from the server and download it as a pdf. The download functionality is working, but unfortunately, the file appears to be corrupt.
My approach involves using JavaScript and vue.js.
Function responsible for calling the server and retrieving the byte array:
function getManualAcademico(id) {
const url = '/Sec/Ent/Get?d=';
window.config.axios.get(url + id)
.then(function (response) {
var manualAcademico = response.data;
Here I am invoking a function to download the PDF by passing in the response data (an array of bytes).
DownloadPDF(manualAcademico);
})
.catch(function(error) {
logInConsole(error);
});
}
function DownloadPDF(manualAcademico) {
var byteArray = new Uint8Array(manualAcademico.manual.$value);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type: 'application/pdf' }));
a.download = "ManualAluno.pdf";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
Regarding the server side code:
[EnableCors(origins: "*", headers: "*", methods: "*")]
public IHttpActionResult Get(int Id)
{
return Ok(new
{
manual = SEC.Get(Id)
});
}
The download process seems to be successful, but the resulting file is corrupted. Any suggestions or insights on how to resolve this issue would be greatly appreciated.
Thank you.