Multer: Check if the upload folder path exists

Created on 10 Dec 2015  路  7Comments  路  Source: expressjs/multer

I would like to know if there is any option to check if upload path exists, if not create the path.

I can do this in _multer.diskStorage -> destination_ function, I would like to do the check using async or promises. Is there any support for this in multer?

Most helpful comment

Actually, you might want to use the mkdirp module:

const mkdirp = require('mkdirp')

multer.diskStorage({
  destination: (req, file, cb) => {
    const dir = '/my/long/path/with/many/levels'

    mkdirp(dir, err => cb(err, dir))
  }
})

All 7 comments

It would be worth thinking about if we should include this straight in multer, in the meantime you can absolutely do it asynchronous.

multer.diskStorage({
  destination: (req, file, cb) => {
    const dir = '/my/nice/path'

    fs.mkdir(dir, err => cb(err, dir))
  }
})

Hope it helps :+1:

Actually, you might want to use the mkdirp module:

const mkdirp = require('mkdirp')

multer.diskStorage({
  destination: (req, file, cb) => {
    const dir = '/my/long/path/with/many/levels'

    mkdirp(dir, err => cb(err, dir))
  }
})

We can also use async by adding a middleware in router before upload

var fs = require('fs')
var uploadPath = ''
app.post('/profile', checkUploadPath, upload.single('avatar'), function (req, res, next){
})

function checkUploadPath(req, res, next) {
     fs.exists(uploadPath, function(exists) {
        if(exists) {
          next();
        }
        else {
          fs.mkdir(uploadPath, function(err) {
            if(err) {
              console.log('Error in folder creation');
              next(); 
            }  
            next();
          })
        }
     })
}

@vishnurs If the uploadPath is static I would recommend doing the check only once instead of _for every_ requests.

const multer = require('multer')
const mkdirp = require('mkdirp')

const uploadPath = '/my/nice/path'
const upload = muter({ dest: uploadPath })

mkdirp.sync(uploadPath)

app.post('/profile', upload.single('avatar'), (req, res, next) => {
  // ...
})

@LinusU Yes. uploadPath is not static in my case, it depends on the request parameters.

:ok_hand:

It would be worth thinking about if we should include this straight in multer, in the meantime you can absolutely do it asynchronous.

multer.diskStorage({
  destination: (req, file, cb) => {
    const dir = '/my/nice/path'

    fs.mkdir(dir, err => cb(err, dir))
  }
})

Hope it helps 馃憤

This will always try to make directory. You don't have a condition that checks whether directory exists or not. With your code, only first time upload will be successful and later uploads will end up in exceptions. Please update your code for checking if directory exists or not.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Gabxi picture Gabxi  路  3Comments

samipjain picture samipjain  路  4Comments

tonghae picture tonghae  路  4Comments

adrienbarreau picture adrienbarreau  路  3Comments

ChristianRich picture ChristianRich  路  4Comments