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'

并试图在我的代码中隐含它。

检查文件上传,我使用邮递员将文件上传到我的网址,但进入请求的时间已超时。

Postman Screenshot

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 (将#修改为@)