上传 multer 后无法保存文件
cant save a file after uploading multer
<form action="/upload" method="post">
<input type="file" name="avatar" id="avatarinput" onchange="readfichier()">
<button type='submit'>Save</button>
</form>
服务器
app.post('/upload', upload.single('avatar'), function (req, res, next) {
console.log(req.file);
fs.rename(req.file.path, 'uploads/'+req.file.originalname, function (err) {
if (err) throw err;
console.log('renamed complete');
});
res.end('Success');
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})
我收到这个错误:
类型错误:无法读取未定义的属性(读取 'path')
我猜这是你唯一的密码。
首先,您必须将 multer 定义为 api
的中间件
然后你必须告诉你的 multer 你想在哪里存储你的图像
我想它在你的磁盘中
所以在你的服务器上代码看起来像这样
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req,file,cb){
cb(null, './uploads')
},
filename: function (req,file,cb){
cb(null,file.fieldname+'-'+Date.now()+'.'+file.mimetype.split('/').reverse()[0]);
},
});
var upload = multer({storage: storage});
app.post('/upload', upload.single('avatar'), function (req, res, next) {
console.log(req.file);
fs.rename(req.file.path, 'uploads/'+req.file.originalname, function (err) {
if (err) throw err;
console.log('renamed complete');
});
res.end('Success');
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})
首先,您需要使用磁盘存储引擎来完全控制将文件存储到磁盘。
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
cb(null,file.fieldname+'-'+Date.now()+'.'+file.mimetype.split('/').reverse()[0]);
}
})
const upload = multer({ storage: storage })
现在,定义您的路由并将 multer 添加为中间件。
app.post('/upload', upload.single('avatar'), function (req, res, next) {
console.log(req.file);
fs.rename(req.file.path, 'uploads/'+req.file.originalname, function (err) {
if (err) throw err;
console.log('renamed complete');
});
res.end('Success');
})
这一点在之前的回答中也有很好的描述。但是您缺少的是将 enctype="multipart/form-data"
添加到 HTML 表单中。详细了解为什么我们需要这样做 here.
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="avatar" id="avatarinput" onchange="readfichier()">
<button type='submit'>Save</button>
</form>
希望这能解决您所有的问题。如果没有,参考这个.
<form action="/upload" method="post">
<input type="file" name="avatar" id="avatarinput" onchange="readfichier()">
<button type='submit'>Save</button>
</form>
服务器
app.post('/upload', upload.single('avatar'), function (req, res, next) {
console.log(req.file);
fs.rename(req.file.path, 'uploads/'+req.file.originalname, function (err) {
if (err) throw err;
console.log('renamed complete');
});
res.end('Success');
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})
我收到这个错误: 类型错误:无法读取未定义的属性(读取 'path')
我猜这是你唯一的密码。
首先,您必须将 multer 定义为 api
的中间件然后你必须告诉你的 multer 你想在哪里存储你的图像
我想它在你的磁盘中
所以在你的服务器上代码看起来像这样
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req,file,cb){
cb(null, './uploads')
},
filename: function (req,file,cb){
cb(null,file.fieldname+'-'+Date.now()+'.'+file.mimetype.split('/').reverse()[0]);
},
});
var upload = multer({storage: storage});
app.post('/upload', upload.single('avatar'), function (req, res, next) {
console.log(req.file);
fs.rename(req.file.path, 'uploads/'+req.file.originalname, function (err) {
if (err) throw err;
console.log('renamed complete');
});
res.end('Success');
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})
首先,您需要使用磁盘存储引擎来完全控制将文件存储到磁盘。
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
cb(null,file.fieldname+'-'+Date.now()+'.'+file.mimetype.split('/').reverse()[0]);
}
})
const upload = multer({ storage: storage })
现在,定义您的路由并将 multer 添加为中间件。
app.post('/upload', upload.single('avatar'), function (req, res, next) {
console.log(req.file);
fs.rename(req.file.path, 'uploads/'+req.file.originalname, function (err) {
if (err) throw err;
console.log('renamed complete');
});
res.end('Success');
})
这一点在之前的回答中也有很好的描述。但是您缺少的是将 enctype="multipart/form-data"
添加到 HTML 表单中。详细了解为什么我们需要这样做 here.
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="avatar" id="avatarinput" onchange="readfichier()">
<button type='submit'>Save</button>
</form>
希望这能解决您所有的问题。如果没有,参考这个