Multer file.filename and file.path undefined

心不动则不痛 提交于 2019-12-11 15:27:54

问题


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?


回答1:


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
});


来源:https://stackoverflow.com/questions/47110048/multer-file-filename-and-file-path-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!