简体   繁体   中英

how to pass data along with files from angular controller to node server?

I am passing some data from angular.js controller in following format:

data: { model: { title: 'hello'}, files: $scope.files}

I am able to get this in node.js by doing this:

req.files

Now I want to send one more data with existing data, so I tried doing this:

data: { model: { title: 'hello', id:newId}, files: $scope.files}

but when I did req.id . I am getting undefined .

What I am doing wrong?

In angular, pass by combining in formdata :

var fd = new FormData();
fd.append("object", JSON.stringify($scope.yourJsonObject)); //pass json here
fd.append("files", $scope.files);

//wild card(*/*) consumes type
$http.post("Your URL", fd, {
    transformRequest: angular.identity,
    headers: {'Content-Type': undefined}
});

Then retrive object and files separately by their names.

In post call the data is present in body of request obj try to find out your data in req obj like this

   console.log(req.body)

on server side

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM