如何解决Firebase Cloud Function上传excel文件以解析数据并将其保存到Firestore
用例:我需要将.xlsx文件上传到云函数API,将数据解析为json格式,然后将其上传到Firebase Firestore。我正在使用打字稿编写云功能。
我阅读了有关multer的信息,并了解到将multer与firebase一起使用存在一些问题。另一种选择是Busboy。
并经过以下示例代码:https://gist.github.com/tonkla/5e893aa8776923ad6a2c9c6b7c432f3d
我要发送给busboy的headers ['content-type']是'application / x-www-form-urlencoded'
并试图在我的代码中隐含它。
要检查文件上传,我使用邮递员将文件上传到我的网址,但进入请求的时间已超时。
export const fileUpload = functions.https.onRequest(async (req,res) => {
const data = await parseMultipartFormData(req.rawBody,req.headers['content-type'])
console.log("Data from busboy:"+data)
const workbook = xlsx.read(data,{type: 'buffer'})
const jsonRows = xlsx.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]])
res.status(200).json(jsonRows)
});
function parseMultipartFormData(rawBody,headers) {
return new Promise((resolve,reject) => {
const buffers : any[]= []
const busboy = new busboyMain({
headers: { 'content-type': headers },})
busboy.on('file',(fieldname,file,filename,encoding,mimetype) => {
file.on('data',data => {
buffers.push(data);
})
file.on('end',() => {
resolve(Buffer.concat(buffers));
})
})
busboy.on('error',error => reject(error))
busboy.end(rawBody)
})
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。