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:

http://i.imgur.com/bb7bofh.png

the req.body empty when logged (note empty object):

enter image description here

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

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -