0

HTML:

<form action="/uploadpic" method="post" enctype="multipart/form-data">
    <input type="file" data-clear-btn="true" name="image" id="new_pic" value="" placeholder="Choose File">
    <input type="submit" value="Add" style="width:30%">
</form>

NodeJS:

app.post('/uploadpic', function(req,res) {
console.log(req.files);
console.log(req.body);});

I also use:

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());
app.use(bodyParser.json())
app.use(express.bodyParser({uploadDir:'./uploads'}));
app.use(bodyParser.json({ type: 'application/vnd.api+json' }))

in the console I get:

{}
{}

i dont seem to understand what could be the problem here.. thanks !

2
  • Which version of Express are you using? I have used BodyParser for file uploads with Express 3* Commented Jul 11, 2014 at 11:05
  • I use Express 3. how do you use body parser for file uploads ? Commented Jul 11, 2014 at 11:07

2 Answers 2

1
   var fs = require('fs');

    app.post('/uploadpic', function(req,res) {

    //req.files contains array of files iterate and get it
    //if it has only one. it is like object

    //here is the code for object

    if (req && req.files) {

     var contentType = req.files.file.type;    
     var fname = req.files.file.name;    
     var image_path = req.files.file.path;    
     fs.readFile(image_path, function (err, data) { 
      var data = data; //this is your data use this
     })

    }

})
Sign up to request clarification or add additional context in comments.

Comments

0

BodyParser doesn't include file uploads. You need to use something like multer or multiparty.

Also express (4.0+) doesn't come bundled with middleware anymore, so you'll need to use bodyparser for POST requests.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.