Upload camera file and normal json data in the same post


#1

I have a function that upload a file to the server and then upload normal json data.

here is my code:

$scope.sendPost = function() {

    //normall data
    var postData = {
      name: shareData.returnData()[0].title,
      content: shareData.returnData()[0].content,
      category1: category1,
      category2: category2,
      category3: category3,
      category4: category4,
      category5: category5,
    };
    
    //upload image post
    var options = {};
    options.fileKey='avatar';
    
    $cordovaFile.uploadFile('http://10.0.0.4:1337/posts/newPost', shareData.returnData()[0].image, options).then(function(result) {
      postService.sendData("http://10.0.0.4:1337/posts/newPost", postData); //after the file upload
    }, function(err) {
      // Error
    }, function (progress) {
      // constant progress updates
    });

  }

this code works but since i need to synth between the file and the data i cannot send them one by one. how can i send data + cordova camera file to the server in one go?


#2

need help with this people…


#3

If you look at the documentation for the cordova file transfer plugin (for which this is a wrapper), you’ll see that you can send additional values in the options parameter.


#4

You can send meta data along with the file bu using the “params” option.
This data will can be retrieved in request.POST on the server end.