简体   繁体   English

如何在通过multer上传文件之前自动创建文件夹,以便这些文件存储在nodejs中创建的文件夹中?

[英]How to create folder automatically before files upload via multer so that those files get stored in that created folder in nodejs?

This is my multer code to upload multiple files.这是我上传多个文件的 multer 代码。

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './public/files/'+ req.user.id)
  },
  filename: function (req, file, cb) {
    x = file.originalname; //+path.extname(file.originalname);
  cb(null,x);
  }
});

var upload = multer({storage: storage});

This is the post request where files get submitted on click submit.这是在单击提交时提交文件的发布请求。

router.post(upload.array("FileUpload",12), function(req, res, next) {

//Here accessing the body datas.

})

So what I want is that, I want to create a folder first with the name of the ID generated which can be access from the req.body and then upload those files into that folder respectively.所以我想要的是,我想首先使用生成的ID的名称创建一个folder ,可以从req.body访问,然后将这些文件分别上传到该文件夹​​中。 But since I cannot access the body first before upload I am unable to create that respective folder directory.但是由于在上传之前我无法先访问body ,因此我无法创建相应的folder directory. Is there any other way around which I can think of and implement this?有没有其他方法可以让我想到并实现这一点?

Updated Solution using fs-extra package.使用 fs-extra 包更新了解决方案。

const multer = require('multer');
let fs = require('fs-extra');

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    let Id = req.body.id;
    fs.mkdirsSync('./public/files/'+ req.user.id + '/' + Id);
    cb(null, './public/files/'+ req.user.id + '/' + Id)
  },
  filename: function (req, file, cb) {
    x = file.originalname; //+path.extname(file.originalname);
  cb(null,x);
  }
});

var upload = multer({storage: storage});

This is the post request where files get submitted on click submit.这是在单击提交时提交文件的发布请求。

router.post(upload.array("FileUpload",12), function(req, res, next) {

//Here accessing the body datas.

})

you have to install first the fs-extra which will create folder你必须先安装fs-extra它将创建文件夹

create seprate folder for multer like multerHelper.js为 multer 创建单独的文件夹,如multerHelper.js

const multer = require('multer');
let fs = require('fs-extra');

let storage = multer.diskStorage({
destination: function (req, file, cb) {
    let Id = req.body.id;
    let path = `tmp/daily_gasoline_report/${Id}`;
    fs.mkdirsSync(path);
    cb(null, path);
},
filename: function (req, file, cb) {
    // console.log(file);

    let extArray = file.mimetype.split("/");
    let extension = extArray[extArray.length - 1];
    cb(null, file.fieldname + '-' + Date.now() + "." + extension);
 }
})

let upload = multer({ storage: storage });

let createUserImage = upload.array('images', 100);

let multerHelper = {
    createUserImage,
}

module.exports = multerHelper;

in your routes import multerhelper file在您的路线中导入 multerhelper 文件

const multerHelper = require("../helpers/multer_helper");

router.post(multerHelper , function(req, res, next) {

//Here accessing the body datas.

})

I followed your code, but it returns undefined in the folder name.我遵循了您的代码,但它在文件夹名称中返回未定义。 How did you get correct id?你是怎么得到正确的身份证的? Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM