Answer the question
In order to leave comments, you need to log in
How to use multer and React?
I need to upload a file to the server (from input) and send the path to the database
. To do this, I use the multer library and the function:
const multer = require('multer')
const moment = require('moment')
const storage = multer.diskStorage({
destination(req, file, cb) {
cb(null, './uploads')
},
filename(req, file, cb) {
const date = moment().format('DDMMYYYY-HHmmss_SSS')
cb(null, `${date}-${file.originalname}`)
}
})
const fileFilter = (req, file, cb) => {
if (file.mimetype === 'image/png' || file.mimetype === 'image/jpeg' || file.mimetype === 'image/jpg') {
cb(null, true)
}
else {
cb(null, false)
}
}
module.exports = multer({
storage,
fileFilter
})
<form action="http://localhost:80/api/server/addArticle" method="POST" enctype="multipart/form-data">
router.post('/addArticle', upload.single('img'), async(req, res) => {
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question