Javascript 如何从ArrayBuffer保存服务器上的.xlsx文件?

Javascript 如何从ArrayBuffer保存服务器上的.xlsx文件?,javascript,node.js,Javascript,Node.js,我有一个XLSX,作为ArrayBuffer从nodejs服务器发送 const result = nodeExcel.execute(data); const body = Buffer.from(result, 'binary'); 我在axios中通过以下参数接收: responseType: arraybuffer headers: {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,i

我有一个XLSX,作为ArrayBuffer从nodejs服务器发送

const result = nodeExcel.execute(data);
const body = Buffer.from(result, 'binary');
我在axios中通过以下参数接收:

responseType: arraybuffer
headers: {"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"}
我试着这样下载:

const buftype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8';
const url = window.URL.createObjectURL(new Blob([res.data]), {
   type: buftype
});
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'data.xlsx');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
let buf = this.a2ab(res.data);
const buftype = 'application/vnd.ms-excel;charset=utf-8';
let blob = new Blob([buf], {
    type: buftype
});
但xlsx文件最终会损坏。将data.xlsx更改为data.csv会得到一行整数。当我在客户端打印阵列时,它最终会变成这样

[80, 75, 3, 4, 10, 0, 0, 0, 8, 0, 252, 113, 209, 80, 120, 168, 205, 70, 60, 1, 0, 0, 180, 4, 0, 0, 19, 0, 0, 0, 91, 67, 111, 110, 116, 101, 110, 116, 95, 84, 121, 112, 101, 115, 93, 46, 120, 109, 108, 173, 148, 221, 78, 2, 49, 16, 133, 95, 101, 211, 91, 179, 91, 240, 194, 24, 195, 194, 133, 122, 171, 36, 250, 2, 181, 157, 101, 27, 250, 151, 206, 128, 240, 246, 14, 197, 160, 65, 3, 10, 220, 108, 179, 157, 51, 231, 59, 253, 73, 71]
在服务器端,它用十六进制值表示

<Buffer 50 4b 03 04 0a 00 00 00 08 00 fc 71 d1 50 78 a8 cd 46 3c 01 00 00 b4 04 00 00 13 00 00 00 5b 43 6f 6e>


问题出在编码上吗?如何在客户端将缓冲区转换为utf8?

因此我意识到,不知何故,数据是作为数组而不是arraybuffer接收的。我通过使用以下命令将数组转换为ArrayBuffer修复了此问题:

a2ab(s) {
    let buf = new Uint8Array(s).buffer;
    return buf;
},
然后像这样使用它:

const buftype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8';
const url = window.URL.createObjectURL(new Blob([res.data]), {
   type: buftype
});
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'data.xlsx');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
let buf = this.a2ab(res.data);
const buftype = 'application/vnd.ms-excel;charset=utf-8';
let blob = new Blob([buf], {
    type: buftype
});

所以我意识到,不知何故,数据是作为数组而不是arraybuffer接收的。我通过使用以下命令将数组转换为ArrayBuffer修复了此问题:

a2ab(s) {
    let buf = new Uint8Array(s).buffer;
    return buf;
},
然后像这样使用它:

const buftype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8';
const url = window.URL.createObjectURL(new Blob([res.data]), {
   type: buftype
});
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'data.xlsx');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
let buf = this.a2ab(res.data);
const buftype = 'application/vnd.ms-excel;charset=utf-8';
let blob = new Blob([buf], {
    type: buftype
});