我正在尝试转换来自服务器的字节数组,并以pdf格式下载,下载正常,但文件已损坏。
我正在尝试通过JavaScript进行操作,而我正在使用vue.js。
调用服务器并带来字节数组的功能:
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 calling the function sending the response data (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);
}
和服务器:
[EnableCors(origins: "*", headers: "*", methods: "*")]
public IHttpActionResult Get(int Id)
{
return Ok(new
{
manual = SEC.Get(Id)
});
}
正在下载,但是文件损坏了,知道吗?
谢谢。
尝试将responseType: 'arraybuffer'
添加到请求中。
喜欢的东西:
window.config.axios.get({url: url + id, responseType: 'arraybuffer'})..
在这种情况下