我正在尝试使用express.js和multer上传文件。我的代码如下所示。
const storage = multer.diskStorage({
destination: function ( req, file, cb ) {
cb( null, './uploads/' );
},
filename: function ( req, file, cb ) {
cb( null, file.originalname+ '-' + Date.now() );
}
});
const fileFilter = ( req, file, cb ) => {
if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png' ) {
cb( null, true );
} else {
cb( new Error('Try to upload .jpeg or .png file.'), false );
}
}
const upload = multer({
storage: storage,
limits: {
fileSize: 1024 * 1024 * 5
},
fileFilter: fileFilter
});
router.post('/upload', upload.single('addressImage'), addressController.uploadImage);
const uploadImage = (req, res, next) => {
// I would like to confirm file uploaded perfectly here.
}
如何确认文件已正确上传?我想返回上传的文件名。