我使用Multer制作上传文件系统。我按照Github页面的说明操作,但它没有工作。我得到了
const express= require('express');
const app = express();
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/uploads');
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now());
}
});
const upload = multer({ storage: storage });
app.post('/editPhoto',upload.single('avatar'),function(req,res,next){
upload(req,res,function(err){
if(err){
res.json({success:false,message:err});
}
else{
res.json({success:true,message:"Photo was updated !"});
}
});
});
我得到TypeError: upload is not a function
我做错了什么?
修改
我按照你说的那样做了,正如医生说的那样。
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads');
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now());
}
});
app.post('/editPhoto',function(req,res,next){
var upload = multer({ storage:storage}).single('userPhoto');
upload(req,res,function(err){
console.log(req.file);
if(err){
res.json({success:false,message:err});
}
else{
res.json({success:true,message:"Photo was updated !"});
}
});
});
req.file未定义
当我尝试这样的时候
var upload = multer({ storage:storage});
app.post('/editPhoto',function(req,res,next){
upload(req,res,function(err){
console.log(req.file);
if(err){
res.json({success:false,message:err});
}
else{
res.json({success:true,message:"Photo was updated !"});
}
});
});
我上传不是功能
答案 0 :(得分:4)
正如@Aabid在评论中所说,你不需要同时使用multer中间件和上传到控制器中。
您可以使用:
app.post('/editPhoto', upload.single('avatar'), (req, res, next) => {
// here in the req.file you will have the uploaded avatar file
})
或者您可以使用:
app.post('/editPhoto', (req, res, next) => {
upload(req, res, function (err) {
if (err) {
// This is a good practice when you want to handle your errors differently
return
}
// Everything went fine
})
})
所以你使用两种方法中的一种,而不是同时使用两种方法。
答案 1 :(得分:0)
// Multer upload config
const fs = require('fs');
const path = require('path');
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/')
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname))
}
})
const upload = multer({storage: storage});
app.post('/uploadPhoto', (req, res, next) => {
upload.single('filename_here')(req, res, function (err) {
if (err) {
// A Multer error occurred when uploading.
res.json({msg: err.message})
} else {
// Everything went fine.
// req.file
// {
// fieldname: 'filename_here',
// originalname: 'nhancv_dep_trai.png',
// encoding: '7bit',
// mimetype: 'image/png',
// destination: '/tmp/',
// filename: 'filename_here-1607694392023-220481630',
// path: '/tmp/filename_here-1607694392023-220481630',
// size: 5907
// }
const file = req.file;
console.log(file);
// Delete tmp
try {
// fs.unlinkSync(file.path);
} catch (e) {
// Ignore
console.error(e);
}
res.json({msg: 'ok'});
}
});
})
测试
curl --location --request POST '<host>:<port>/uploadPhoto' \
--form 'filename_here=@"/Users/nhancv/Desktop/nhancv_dep_trai.png"'
答案 2 :(得分:-1)
var multer = require('multer');
var upload = multer({ dest: './temp/hh' })
var cpUpload = upload.fields([{ name: 'audio_file', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/v1/index/uploads', cpUpload, function (req, res, next) {
console.log(req.files['audio_file'][0]);
console.log(req.files['gallery']);
})