1

I am working with a simple file upload from client to server . Below is what i tried.

    eReaderBook.controller('browseCtrl',['$scope',"$http",function($scope,$http){
$scope.fileName ="";
$scope.imageName ="";
    $scope.uploadFile = function(){

        var data1 = new FormData();
        data1.append('input_file_name', $('#fileName').prop('files')[0]);
        data1.append('input_image_name', $('#imageName').prop('files')[0]);
        if($scope.fileName!="" && $scope.imageName!="")
        {

            $http({
                url: '/upload',
                method: "POST",
                data: data1
            })
            .then(function(response) {
                    console.log("success")
            }, 
            function(response) { // optional
                    console.log("failed")
            }); 
        } 
    };
}])

node .js

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer());
app.post('/upload',function(req,res)
{
   console.log("aaa");  
   res.send("hi")
 });

But when i send request to nodejs server it throws an error Error: invalid json at parse (d:\MeanTest\node_modules\body-parser\lib\types\json.js:83:15)

What i am doing wrong here...

1
  • try using just this - app.use(bodyParser); Commented Aug 4, 2015 at 7:49

1 Answer 1

1

The problem with your code is that the data you are passing is not a valid json and so bodyParser.json() while processing, will throw an error.

You can use bodyParser.raw(options) to use as a middleware. The bodyParser.raw() returns middleware that parses all bodies as a Buffer.

For reference: https://github.com/expressjs/body-parser

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

1 Comment

@bharath: Happy to help :-)

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.