Multer:文件路径不正确
Multer: path of the file isn't right
我将 multer 添加到我的节点 js 应用程序并且它工作正常,只是我需要存储在数据库中的图像路径不正确。找不到问题,这显然是我犯的一些愚蠢的错误。
这是我对 multer 的设置
const multer = require('multer');
const storage = multer.diskStorage({
destination: './public/images',
filename: function(req, file, next){
next(null, Date.now() + '-' + file.originalname);
}
});
const upload = multer({ storage: storage});
下面是我如何使用它来存储路径
router.post('/add', upload.single('myImage'), function(req, res){
req.checkBody('title','Title is required').notEmpty();
//req.checkBody('author','Author is required').notEmpty();
req.checkBody('body','Body is required').notEmpty();
// Get Errors
let errors = req.validationErrors();
if(errors){
res.render('add_article', {
title:'Add Article',
errors:errors
});
} else {
let article = new Article();
//var date = new Date();
article.title = req.body.title;
article.author = req.user._id;
article.body = req.body.body;
article.descript = req.body.descript;
article.category = req.body.category;
article.date = getDate();
article.time = getTime();
article.comments = 0;
article.img = req.file.path;
console.log(req.file);
article.save(function(err){
if(err){
console.log(err);
return;
} else {
req.flash('success','Article Added');
res.redirect('/');
}
});
}
});
从这里可以看出路径不对,我不能在GET上使用它
{ _id: 5bd993756373a5182460aa2a,
title: 'Sport 5',
author: '5acab056708e0d1248cba6ed',
body: 'sadddddddddddddd213',
descript: 'dsadas',
category: 'sport',
date: '2018/10/31',
time: '12:35',
comments: 0,
img: 'public\images\1540985717747-nike_logo_slogan_sport_advertising_42643_1280x1024.jpg',
__v: 0 }
Multer 在您这边正常工作,您只需将系统路径转换为 url 可访问的路径。
这对你有帮助。
article.comments = 0;
let fileUrl = req.file.path.replace(/\/g, "/").substring("public".length);
article.img = fileUrl;
这发生在 windows 中,因为 windows 中的任何文件路径都只有反斜杠,但要使文件可以通过 url 访问,它必须有前斜杠。所以只需使用下面的代码将所有反斜杠转换为前斜杠
const imageUrl = req.file.path.replace("\", "/");
我将 multer 添加到我的节点 js 应用程序并且它工作正常,只是我需要存储在数据库中的图像路径不正确。找不到问题,这显然是我犯的一些愚蠢的错误。
这是我对 multer 的设置
const multer = require('multer');
const storage = multer.diskStorage({
destination: './public/images',
filename: function(req, file, next){
next(null, Date.now() + '-' + file.originalname);
}
});
const upload = multer({ storage: storage});
下面是我如何使用它来存储路径
router.post('/add', upload.single('myImage'), function(req, res){
req.checkBody('title','Title is required').notEmpty();
//req.checkBody('author','Author is required').notEmpty();
req.checkBody('body','Body is required').notEmpty();
// Get Errors
let errors = req.validationErrors();
if(errors){
res.render('add_article', {
title:'Add Article',
errors:errors
});
} else {
let article = new Article();
//var date = new Date();
article.title = req.body.title;
article.author = req.user._id;
article.body = req.body.body;
article.descript = req.body.descript;
article.category = req.body.category;
article.date = getDate();
article.time = getTime();
article.comments = 0;
article.img = req.file.path;
console.log(req.file);
article.save(function(err){
if(err){
console.log(err);
return;
} else {
req.flash('success','Article Added');
res.redirect('/');
}
});
}
});
从这里可以看出路径不对,我不能在GET上使用它
{ _id: 5bd993756373a5182460aa2a,
title: 'Sport 5',
author: '5acab056708e0d1248cba6ed',
body: 'sadddddddddddddd213',
descript: 'dsadas',
category: 'sport',
date: '2018/10/31',
time: '12:35',
comments: 0,
img: 'public\images\1540985717747-nike_logo_slogan_sport_advertising_42643_1280x1024.jpg',
__v: 0 }
Multer 在您这边正常工作,您只需将系统路径转换为 url 可访问的路径。
这对你有帮助。
article.comments = 0;
let fileUrl = req.file.path.replace(/\/g, "/").substring("public".length);
article.img = fileUrl;
这发生在 windows 中,因为 windows 中的任何文件路径都只有反斜杠,但要使文件可以通过 url 访问,它必须有前斜杠。所以只需使用下面的代码将所有反斜杠转换为前斜杠
const imageUrl = req.file.path.replace("\", "/");