S3使用Node.js上传Excel数据
我正在尝试使用Node.js和aws sdk将excel文件上载到S3 输入是JSON,我使用XLSX库将其转换为工作簿,并使用以下代码上传到S3S3使用Node.js上传Excel数据,node.js,excel,amazon-s3,aws-sdk,xlsx,Node.js,Excel,Amazon S3,Aws Sdk,Xlsx,我正在尝试使用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, shee
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);
});
}
const wb=XLSX.utils.book_new();
//将JSON转换为工作表数据
const sheetData=XLSX.utils.json\u to\u sheet(请求正文);
XLSX.utils.book_append_sheet(wb,sheetData,“sheet 1”);
const sheetDataBuffer=XLSX.write(wb,{bookType:'XLSX',type:'buffer',bookst:false});
const s3=新的AWS.s3();
s3.上传({
密钥:文件,
桶:,
正文:sheetDataBuffer,
ContentType:“application/vnd.openxmlformats of icedocument.spreadsheetml.sheet”,
ContentEncoding:'base64'
}).承诺
。然后(数据=>{
logger.debug(“上传数据”);
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;
}
});
Papa.parse(e.target.files[0]{
编码:“utf-8”,
下载:对,
完成:异步数据=>{
//数据。数据是乱码的
const response=等待获取(“/”{
方法:“POST”,
标题:{
“内容类型”:“应用程序/json”,
},
正文:data.data
});
const res=等待响应;
}
});
因此,我在客户端使用XSLX库(不再使用Papaparse)解决了这个问题,以防它对某人有所帮助
const reader = new FileReader();
reader.onload = async (evt) => {
const bstr = evt.target.result;
const wb = XLSX.read(bstr, {type:'binary'});
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
//data is parsed correctly
const data = XLSX.utils.sheet_to_json(ws, {header:1});
const response = await fetch('/<apipath>', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
const res = await response;
};
reader.readAsBinaryString(e.target.files[0]);
const reader=new FileReader();
reader.onload=异步(evt)=>{
const bstr=evt.target.result;
常量wb=XLSX.read(bstr,{type:'binary'});
const wsname=wb.SheetNames[0];
常量ws=wb.Sheets[wsname];
//正确解析数据
const data=XLSX.utils.sheet_to_json(ws,{header:1});
const response=等待获取(“/”{
方法:“POST”,
标题:{
“内容类型”:“应用程序/json”,
},
正文:JSON.stringify(数据)
});
const res=等待响应;
};
reader.readAsBinaryString(e.target.files[0]);