Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multer file.filename and file.path undefined

I'm trying to upload/store images on disk memory using express and multer. My code in a nutshell below:

const express = require('express')
const multer = require('multer');
const upload = multer({ 
    destination: function(req, file, cb) {
        cb(null, 'uploads/')
    },
    filename: function(req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
});
app.post('/', upload.single('photo'), function(req, res) {
    console.log(req.file);
});

Unfortunately, when I use Postman to send a POST request (the image has the correct fieldname of "photo"), the filename function does not seem to affect the name. The file ends up in /uploads, but with the default naming scheme from multer (not my custom name defined in the filename function).

Also, my console.log(req.file) line outputs this:

{ fieldname: 'photo',
  originalname: 'test.jpg',
  encoding: '7bit',
  mimetype: 'image/jpeg',
  buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 00 00 01 00 01 00 00 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f ... >,
  size: 85404 }

Note that there is no value defined for req.file.filename, req.file.destination or req.file.path as specified in the multer API documentation.

Any idea what I'm doing wrong here?

like image 320
Joey M-H Avatar asked Mar 07 '23 16:03

Joey M-H


2 Answers

Ok, answered my own question. In case it's useful for someone else, my problem was:

I did not use multer.diskStorage() to define the destination and filename. You need to use the .diskStorage() method when you want to supply these values. The correct usage would have been:

const multer = require('multer');
const storage = multer.diskStorage({ // notice you are calling the multer.diskStorage() method here, not multer()
    destination: function(req, file, cb) {
        cb(null, 'uploads/')
    },
    filename: function(req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now())
    }
});
const upload = multer({storage}); //provide the return value from 

Then you can use multer normally as middleware, e.g.:

app.post('/', upload.single('photo'), function(req, res) {
    // do what you want with req.file
});
like image 189
Joey M-H Avatar answered Mar 21 '23 00:03

Joey M-H


Also keep in mind that if you want to access the original name (name of the file sent over the network) you must use file.originalname instead of file.filename.

like image 22
Mahdi Ghajary Avatar answered Mar 20 '23 23:03

Mahdi Ghajary