Zalaboza Zalaboza - 2 months ago 30
AngularJS Question

post multiple files in one request in angular cordova app

searched every where for a solution since backend which my app contacts require posting all files in one request.

my current code:-> -angularjs 1.5.3-

app.service('siUpload',function($q){

//...

this.upload(imageURI,url){
var deferred = $q.defer();

//.. etc.. options and other stuff
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI(url),deferred.resolve,deferred.reject);

return deferred.promise
}

})


and in controller i can loop files selected and upload them one by one.

angular.forEach($scope.files,function(f){
siUpload.upload(f.url,$server+'blog/'+e.grouping+'/file')
});


This works perfectly, yet server requires to send all files in single request under name
file[]
.

before i used angular lib ngFileUpload yet it doesn't work on android 4.4.2 any more because of a webview bug in input type = file :(.

so is there any how cordova-file-transfer can send multiple files in single request ? or even use native xhr to upload multiple files in single request ?

Answer

since i got no answers, and couple upvotes, this is how i ended up solving this problem.

Note i'm using angularjs 1.x

  1. in my service i added a function to appends files to FormData object.
  2. in controller i submit this FormData using $http.

code:

app.service('siUpload',function(){

var formData = new FormData();
var collection = [];
var options = {
  quality: 80,
  destinationType: Camera.DestinationType.FILE_URL,
  sourceType: Camera.PictureSourceType.CAMERA
};



this.pickFiles = function(){
    var defer = $q.defer();//Create a PROMISE
//openup Camera to Capture files
navigator.camera.getPicture(
function(imageURI){
    //add captured file to FormData
    window.resolveLocalFileSystemURL(imageURI, function(fileEntry) {
        fileEntry.file(function(file) {
            var reader = new FileReader();

            //add to collection for thumbnail views
            collection.push(file.nativeURL);

            reader.onloadend = function(fileReadResult) {
                var data;
                data = new Uint8Array(fileReadResult.target.result);
                //add to formData for submitting
                formData.append('file[]', new Blob([data], {
                    type: file.type
                    }), file.name);

                //return [0]=>collection of thumbnails [1]=>formData to be posted
                defer.resolve([collection,formData]);
            };

            defer.notify('got image, appending it to formData');
            reader.readAsArrayBuffer(file);
        });

    });
},
function(message){
defer.reject('Camera rejected'+message);
},options);

return defer.promise;

}

using this function i can trigger in my controller

$scope.capture = function(){

  siUpload.pickFile().then(
    function(r){
      //r contain thumbnails and form data
      $scope.thumbnails = r[0];

      var fd = r[1];
      angular.forEach($scope.post,function(val,key){
       fd.append(key,val); //adding extra paramters to the post request
      });

      //POSTING All FILES and data in one request :)
      $http.post('API://blog', fd, {
            headers: {'Content-Type': undefined },
            transformRequest: angular.identity
      });
    });

}

hope this help future googlers :-)