[英]Multiple file uploads in nodejs using express-fileupload?
Currently I am able to post this to mongodb.目前我可以将其发布到 mongodb。 It works fine.它工作正常。
PROBLEM Instead of one attachment, I should be able to post many attachments independent of each other, there will be N different buttons available for the N uploads.问题不是一个附件,我应该能够发布许多彼此独立的附件,N 个上传将有 N 个不同的按钮可用。
const form = (req, res, next) => {
const file = req.files.photo;
file.name = `photo_${Math.random(0, 10012)}-${Math.random(0, 2000)}${
path.parse(file.name).ext
}`;
file.mv(`./public/uploads/${file.name}`, async (err) => {
if (err) {
console.error(err);
return res.status(500).json({
message: `Problem With File Upload`,
});
}
const upload = await Form.create({
approved: req.body.approved,
email_: req.body.email,
formData: {
name: "req.body.formData.claimantName",
nationality: "req.body.formData.claimantNationality",
address: "req.body.formData.claimantAddress",
email: "req.body.formData.claimantEmail",
},
fileOne: file.name1,
// these are the next
// fileTwo: req.body.formData.name2,
// fileThree: req.body.formData.name3,
});
return res.status(200).json({
success: true,
message: `File Uploaded Successfully`,
path: file.name,
});
});
};
router.route("/add").post(form);
I tried moving const upload = await Form.create(...)
outside the file.mv(...)
block and do somthing like this``` const file1 = req.files.photo1;我尝试将const upload = await Form.create(...)
file.mv(...)
块之外并执行类似这样的操作``` const file1 = req.files.photo1;
file1.name = photo_${Math.random(0, 10012)}-${Math.random(0, 2000)}${ path.parse(file1.name).ext }
; file1.name = photo_${Math.random(0, 10012)}-${Math.random(0, 2000)}${ path.parse(file1.name).ext }
;
It doesn't work properly.
I think you can fetch the uploaded files in the request
object in your node server on the endpoint which receives the request in req.files
object.我认为您可以在接收req.files
对象中的请求的端点上的节点服务器中的request
对象中获取上传的文件。
Your endpoint should catch files like this您的端点应该捕获这样的文件
const uploads = Object.values(req.files);
As you're going to get an object of key value pairs as key
will be file name value
will be file data object containing name etc. Object.values
will convert that object to an array of objects having the information of uploaded files.因为您将获得一个键值对的对象,因为key
将是文件名, value
将是包含名称等的文件数据对象。 Object.values
将该对象转换为具有上传文件信息的对象数组。 After that create a promise
that will catch the files like shown below:之后创建一个promise
,将捕获如下所示的文件:
Function to handle file uploads处理文件上传的函数
handleFileUpload = (uploads) => {
return new Promise((resolve, reject) => {
const dbData = [];
uploads.forEach(async (upload) => {
const name = await `photo_${Math.random(0, 10012)}-${Math.random(0, 2000
)}${path.parse(upload.name).ext}`;
dbData.push(name);
await upload.mv(`your-path/${name}`, async (err) => {
if (err) {
reject("Something wrong");
}
});
});
resolve(dbData);
});
};
After that pass in the uploads to the promise and make a db insertion
in the then
block.之后将上传传递给承诺并在then
块中insertion
一个数据库。
handleFileUpload(uploads).then((response) => { ... }).catch((e) => { ... });
The response
will have all the image paths. response
将包含所有图像路径。
Note : I'm considering the files to be IMAGES
注意:我正在考虑将这些文件作为IMAGES
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.