Multer file.filename 和 file.path 未定义
Multer file.filename and file.path undefined
我正在尝试使用 express 和 multer upload/store 磁盘内存中的图像。简而言之,我的代码如下:
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);
});
不幸的是,当我使用 Postman 发送 POST 请求时(图像具有正确的字段名 "photo"),filename
功能似乎不会影响名称。该文件最终位于 /uploads 中,但使用 multer 的默认命名方案(不是我在文件名函数中定义的自定义名称)。
此外,我的 console.log(req.file)
行输出如下:
{ 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 }
请注意,multer API documentation 中指定的 req.file.filename
、req.file.destination
或 req.file.path
未定义 值。
知道我做错了什么吗?
好的,回答了我自己的问题。如果它对其他人有用,我的问题是:
我没有使用 multer.diskStorage() 来定义目的地和文件名。当您想要提供这些值时,您需要使用 .diskStorage()
方法。正确的用法应该是:
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
然后就可以正常使用multer作为中间件了,例如:
app.post('/', upload.single('photo'), function(req, res) {
// do what you want with req.file
});
另外请记住,如果您想访问原始名称(通过网络发送的文件的名称),您必须使用 file.originalname
而不是 file.filename
。
我正在尝试使用 express 和 multer upload/store 磁盘内存中的图像。简而言之,我的代码如下:
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);
});
不幸的是,当我使用 Postman 发送 POST 请求时(图像具有正确的字段名 "photo"),filename
功能似乎不会影响名称。该文件最终位于 /uploads 中,但使用 multer 的默认命名方案(不是我在文件名函数中定义的自定义名称)。
此外,我的 console.log(req.file)
行输出如下:
{ 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 }
请注意,multer API documentation 中指定的 req.file.filename
、req.file.destination
或 req.file.path
未定义 值。
知道我做错了什么吗?
好的,回答了我自己的问题。如果它对其他人有用,我的问题是:
我没有使用 multer.diskStorage() 来定义目的地和文件名。当您想要提供这些值时,您需要使用 .diskStorage()
方法。正确的用法应该是:
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
然后就可以正常使用multer作为中间件了,例如:
app.post('/', upload.single('photo'), function(req, res) {
// do what you want with req.file
});
另外请记住,如果您想访问原始名称(通过网络发送的文件的名称),您必须使用 file.originalname
而不是 file.filename
。