我在一个nodejs项目中有一个API,如下所示,它从UI:接收多个附件
const upload = multer()
router.post('/upload', upload.array("attachments"),controller.getSomething);
getSomething应该只使用Axios调用另一个POST API,该API位于另一个NodeJ项目中,该项目接受这些附件并对其进行处理。它还可以通过multer接受多个文件。
我不确定如何将多个文件作为请求从一个Nodejs项目同时发送到另一个。你能帮我一个忙吗。
我不得不将表单数据设置如下:
const formData=new FormData();
for(let file of req.files){
formData.append("attachments",file.buffer,file.originalname);
}
并通过axios将表单数据传递给其他api。
您可以执行以下步骤:
-
上传临时文件(来自UI(时,将其保存在临时文件夹中。
-
使用Axios将所有文件名传递到POST API。
-
在后API中,读取临时文件夹中的所有文件,并将它们流式传输到目的地。
controller.getSomething = (req, res, next) => {
// get the file names
const fileNames = req.files.map(filename => filename);
// now post this filenames to the
axios.post('/pathname', {fileNames})
// or, you can use get request
}
正在阅读帖子中的文件请求:
var promises= ['file1.css', 'file2.css'].map(function(_path){
return new Promise(function(_path, resolve, reject){
fs.readFile(_path, 'utf8', function(err, data){
if(err){
console.log(err);
resolve(""); //following the same code flow
}else{
resolve(data);
}
});
}.bind(this, _path));
});
Promise.all(promises).then(function(results){
//Put your callback logic here
response.writeHead(200, {"Content-Type": "text/css"});
results.forEach(function(content){response.write(content)});
response.end();
});
#从该链接复制。你应该检查能帮助你的不同答案。