Node.js 带有附加数据的Node Express上载文件
我是NodeJS的新手,我想为上传图像创建RESTAPI,并面临这个问题 我想创建一个包含多个文件和来自输入的附加数据的post方法。这是我的代码: index.js:Node.js 带有附加数据的Node Express上载文件,node.js,express,Node.js,Express,我是NodeJS的新手,我想为上传图像创建RESTAPI,并面临这个问题 我想创建一个包含多个文件和来自输入的附加数据的post方法。这是我的代码: index.js: app.post('/upload-photos', upload.array('photos'), function (req, res) { const uploadInfo = req.files.map(file => { return { sourceName: file.original
app.post('/upload-photos', upload.array('photos'), function (req, res) {
const uploadInfo = req.files.map(file => {
return {
sourceName: file.originalname,
newName: file.filename
};
});
res.send(uploadInfo);
});
我的问题是我想添加一些表单数据,如(姓名、地址、电话),是否有其他人遇到过这种情况,请帮助我
谢谢。使用
multer
时,可以通过req.body
访问其他字段
app.post('/upload-photos', upload.array('photos'), function (req, res) {
const { name, address, phone } = req.body;
const uploadInfo = req.files.map(file => {
return {
sourceName: file.originalname,
newName: file.filename
};
});
res.send(uploadInfo);
});
以您的形式:
<form action="/upload-photos" method="post" enctype="multipart/form-data">
<input type="file" name="photos" multiple />
<input type="text" name="name" />
<input type="text" name="address" />
<input type="text" name="phone" />
</form>