开发者社区> 问答> 正文

如何通过multipart / formData请求将文件保存在Firestore数据库中?

我有一个firebase功能:

//Functions and firestore requirements are here

exports.saveData = functions.https.onRequest(async (req, res) => { console.log("Data received") const busboy = new Busboy({headers: req.headers}); const fields = {} const uploads = {}

//Push fields that are not file in fields busboy.on('field', (fieldname, val) => { console.log(Processed field ${fieldname}: ${val}.); fields[fieldname] = val; }); //Push files in uploads busboy.on('file', (fieldname, file, filename) => { console.log('File :', file); const filepath = path.join(tmpdir, filename); uploads[fieldname] = filepath; }); busboy.on('finish', () => { console.log(uploads) console.log(fields) db.collection("a_collection").add({ a: fields.a, b: fields.b, file: "Help ! From the client, I send an image. Which value do I need to save ?" }) .then(function () { res.send("Data is saved") }) .catch(function (error) { res.status(400).send(error) console.error("Error :" + error) }) });

busboy.end(req.rawBody);

}) 运行代码段展开摘要

展开
收起
被纵养的懒猫 2019-09-29 18:09:13 496 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
DTCC 2022大会集锦《云原生一站式数据库技术与实践》 立即下载
阿里云瑶池数据库精要2022版 立即下载
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载