【发布时间】:2021-05-26 12:07:15
【问题描述】:
我正在尝试使用 Node.js 和 aws-sdk 将 excel 文件上传到 S3
输入是 JSON,我正在使用 XLSX 库将其转换为工作簿并使用以下代码上传到 S3。
const wb = XLSX.utils.book_new();
//Convert JSON to sheet data
const sheetData = XLSX.utils.json_to_sheet(req.body);
XLSX.utils.book_append_sheet(wb, sheetData, 'Sheet 1');
const sheetDataBuffer = XLSX.write(wb, {bookType: 'xlsx', type: 'buffer', bookSST: false});
const s3 = new AWS.S3();
s3.upload({
Key: file,
Bucket: <bucket name>,
Body: sheetDataBuffer,
ContentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
ContentEncoding: 'base64'
}).promise()
.then(data => {
logger.debug("uploaded the data");
res.sendStatus(200);
});
}
但是,当我在 S3 上查看上传的文件时,它显示乱码/损坏的数据。我错过了什么?这是内容编码问题吗?
更新:看起来我正在使用的客户端解析库——Papaparse——将excel内容解析为乱码。我尝试将编码选项设置为“utf-8”,但没有帮助。知道我错过了什么吗?
Papa.parse(e.target.files[0], {
encoding: 'utf-8',
download: true,
complete: async data => {
// data.data is garbled
const response = await fetch('/<apipath>', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: data.data
});
const res = await response;
}
});
【问题讨论】:
标签: node.js excel amazon-s3 aws-sdk xlsx