node.js - How to get FormData in Express js? -
i'm trying upload file express server. client code looks this:
axios.post('localhost:3030/upload/audio/', formdata)
and in express server:
app.use(bodyparser.urlencoded({ extended: true })); app.use(bodyparser.json()); app.post('/upload/audio/', function uploadaudio(req, res) { let quality = ['320', '128']; let file = req.body; console.log(file) res.send('frick') }
however, though mp3 file sent:
the req.body
empty when logged (note empty object):
how can formdata (and file) in express.js?
as @tomalak said body-parser
not handle multipart bodies.
so need use third party module suggest use awesome module multer
i tried code, hope can you
app.post('/upload/audio/', function uploadaudio(req, res) { var storage = multer.diskstorage({ destination: tmpuploadspath }); var upload = multer({ storage: storage }).any(); upload(req, res, function(err) { if (err) { console.log(err); return res.end('error'); } else { console.log(req.body); req.files.foreach(function(item) { console.log(item); // move file destination }); res.end('file uploaded'); } }); });
Comments
Post a Comment