简体   繁体   English

使用 express-fileupload 在 nodejs 中上传多个文件?

[英]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.

相关问题 使用 express-fileupload 在 NodeJs 中上传文件无法正常工作 - File Upload in NodeJs with express-fileupload not working properly 如何从不同的上传点在同一页面上上传多个文件。 使用 nodejs express-fileupload - How to upload more than one file on the same page from different upload point. Using nodejs express-fileupload 如何使用 express-fileupload 上传图片? - How to upload image using express-fileupload? Node JS-如何在使用express-fileupload时限制文件大小 - Node JS- How can I limit file size while using express-fileupload 使用 express-fileupload 和 multer 包未定义 Req.file - Req.file undefined with express-fileupload and multer packages 如何将 express-fileupload 创建的缓冲区保存到文件中? - How can I save a buffer created by express-fileupload to a file? 使用express-fileupload后req.files未定义 - req.files undefined after using express-fileupload express-fileupload 图片上传不工作 - express-fileupload image upload not working Express express-fileupload ENOENT:没有这样的文件或目录,打开'public/images/name.ext - Express express-fileupload ENOENT: no such file or directory, open 'public/images/name.ext 如何在 express-fileupload 中上传带有 label 或 alt 值等附加信息的图像文件? - How to upload image file with additional information like label or alt value in express-fileupload?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM