2

I am creating a web app using mean stack where any user can upload an image and it would be stored in the mongo db or in a folder in the server system. I am using the angular-file-upload for uploading the image. The problem comes in the nodejs part where when i try to print the req.body it is showing an empty object.

HTMl code: <input type="file" ng-file-select="onFileSelect($files)" >

Angular code:

$scope.onFileSelect = function($files) {
        for (var i = 0; i < $files.length; i++) {
          var file = $files[i];
          console.log(file);
          $scope.upload = $upload.upload({
            url: '/imagePost/'+Global.user._id, 
             method: 'POST' ,

            data: {myObj: 'image'},
            file: file, 
          }).progress(function(evt) {
            console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
          }).success(function(data, status, headers, config) {
            console.log('success');
          });
          }

      };

Node code:

app.post('/imagePost/:userid',function(req,res){

 console.log('I came here');
 console.log(req.body);
 res.send('');
});

Here i just try to print the requestbody but i get an empty object. I included all the dependencies. If this doesnt work what I feel doing is reading the image and sending it in request and creating a new image in the node code which can be later stored in a folder. But why am I not getting any object in request body. Please help me..

5
  • It won't be in req.body, it'll be in req.files. Commented Jun 4, 2014 at 10:39
  • even i tried req.files and in this case I got undefined Commented Jun 4, 2014 at 10:41
  • Can you add the part of express where you've added your dependencies please (app.use()). Commented Jun 4, 2014 at 10:43
  • app.use(expressValidator()); app.use(bodyParser()); app.use(methodOverride()); app.use(cookieParser()); Commented Jun 4, 2014 at 11:07
  • is it a better idea to read the image and send the data via http and create a new image in node..? Commented Jun 4, 2014 at 11:17

1 Answer 1

1

I think you are using Express v4.x

if yes, you get empty object in req.body, because starting from Express 4, bodyParser is deprecated and replaced with body-parser.

Read about the migration from v3 to v4 here

  • body-parser

It only support urlencoded and json, multipart is no longer supported, if you want to use multipart, you need to add it seperately

You need multipart which used to be supported by bodyParser, for receiving files from your client side

You can either use any of the below;

  1. busboy (recommended because it is fast)
  2. multiparty
  3. mutler (wrapper for busboy, make your life easier coding
  4. formidable
Sign up to request clarification or add additional context in comments.

2 Comments

thanx for ur answer I already solved the problem using mutler and its really easier to code...I have a doubt is there a way to send both data and files in a single http post...? without using angular-file-upload
you need to use angular-file-upload or any custom directive, because AngularJS doesnt support file uploade, you can try send a JSON in the data, example: data: {myObj: 'image', data: 'yourData'}

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.