How to set different destinations in nodejs using multer?

后端 未结 5 1860
你的背包
你的背包 2021-01-31 22:11

I\'m trying to upload any file using Multer package. It\'s working fine when I use following code in my server.js file.

var express         


        
5条回答
  •  时光取名叫无心
    2021-01-31 22:54

    Update

    Quite a few things have changed since I posted the original answer.

    With multer 1.2.1.

    1. You need to use DiskStorage to specify where & how of the stored file.
    2. By default, multer will use the operating system's default directory. In our case, since we are particular about the location. We need to ensure that the folder exists before we could save the file over there.

    Note: You are responsible for creating the directory when providing destination as a function.

    More here

    'use strict';
    
    let multer = require('multer');
    let fs = require('fs-extra');
    
    let upload = multer({
      storage: multer.diskStorage({
        destination: (req, file, callback) => {
          let type = req.params.type;
          let path = `./uploads/${type}`;
          fs.mkdirsSync(path);
          callback(null, path);
        },
        filename: (req, file, callback) => {
          //originalname is the uploaded file's name with extn
          callback(null, file.originalname);
        }
      })
    });
    
    app.post('/api/:type', upload.single('file'), (req, res) => {
      res.status(200).send();
    });
    

    fs-extra for creating directory, just in case if it doesn't exists

    Original answer

    You can use changeDest.

    Function to rename the directory in which to place uploaded files.

    It is available from v0.1.8

    app.post('/api/:type', multer({
    dest: './uploads/',
    changeDest: function(dest, req, res) {
        var newDestination = dest + req.params.type;
        var stat = null;
        try {
            stat = fs.statSync(newDestination);
        } catch (err) {
            fs.mkdirSync(newDestination);
        }
        if (stat && !stat.isDirectory()) {
            throw new Error('Directory cannot be created because an inode of a different type exists at "' + dest + '"');
        }
        return newDestination
    }
    }), function(req, res) {
         //set your response
    });
    

提交回复
热议问题