angularjs ng-resource异步上传

angularjs ng-resource Asynchronously Upload

本文关键字:异步 ng-resource angularjs      更新时间:2023-09-26

我一次上传大约10张照片(每张照片从800kb到1000kb不等)。

我的问题是,应用程序在大约5秒后崩溃。

我如何异步上传照片,并可能提高应用程序的性能?

控制器

  appcon.controller('myCtrl', function($scope, $state, $cordovaCamera, $ionicPopup, PostImg){

      $scope.postData = [];
              var truckid = "%" + window.localStorage['truckid'] + "%";
      $scope.start = function() {
      document.addEventListener("deviceready", function () {
        var options = {
          quality: 200,
          destinationType: Camera.DestinationType.DATA_URL,
          sourceType: Camera.PictureSourceType.CAMERA,                                  
          allowEdit: true,
          encodingType: Camera.EncodingType.JPEG,
          targetWidth: 1000,
          targetHeight: 1000,
          popoverOptions: CameraPopoverOptions,
          saveToPhotoAlbum: false,
          correctOrientation:true
        };
        $cordovaCamera.getPicture(options).then(function(imageData) {
        var file = imageData;
          $scope.postData.push({file});
        }, function(err) {
        console.log('4 error');
        });
      }, false);

     }
     $scope.upload = function() {
       var confirmPopup = $ionicPopup.confirm({
         title: 'Continue?',
         template: 'Are you sure you want to Send Images?'
       });
       confirmPopup.then(function(res) {
         if(res) {
          $scope.postData.push({truckid});
          var post = new PostImg($scope.postData);
          post.$save(function(postObject) {
            var alertPopup = $ionicPopup.alert({
                              title: 'Images Send !',
                              buttons: [{ 
                              text: 'Continue with Check In?',
                              type: 'button-positive'
                              }]
                            });
                            alertPopup.then(function(res) {
                            });  
            });
          }  
          else {
                  console.log('You are not sure');
               }
        });
       }
      })

appcon.factory('PostImg', function($resource) {
 return $resource('http://192.168.0.1/Service.svc/BOB');
});

你可以试试cordova-plugin-file-transfer吗?请注意,它将只在手机上工作。

http://ngcordova.com/docs/plugins/fileTransfer/