var ft = new FileTransfer()在iOS模拟器中不起作用。 CordovaFileTransfer插件问题

时间:2015-11-06 08:53:07

标签: ios angularjs cordova ionic-framework cordova-plugins

我想将我的图像从ios模拟器中的照片库上传到我的amzazon s3存储桶。这段代码在android上工作正常,但拒绝在iOS上工作,我不知道为什么

从图库中选择照片的代码

  $scope.choosePhoto = function(index) {
     var options = {
      destinationType : Camera.DestinationType.FILE_URL,
      sourceType : Camera.PictureSourceType.PHOTOLIBRARY,
      allowEdit : false,
      encodingType: Camera.EncodingType.JPEG,
      popoverOptions: CameraPopoverOptions,
      mediaType: Camera.MediaType.PICTURE,
      correctOrientation: true
    };

   // 3
    $cordovaCamera.getPicture(options).then(function(imageData) {
   // 4
      var imagetype; 
      onImageSuccess(imageData);
      function onImageSuccess(fileURI) {
        createFileEntry(fileURI);  
      }

      function createFileEntry(fileURI) {
        window.resolveLocalFileSystemURL(fileURI, copyFile, fail);
      }

       // 5
      function copyFile(fileEntry) {
        var name = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('/') + 1);
        var newName = (new Date()).getTime() + name;
         halfthru(fileEntry, newName); //diff
         getImageType(fileEntry); //diff

      }

      function getImageType(fileEntry) { //diff
        var typeImage;
        $scope.$evalAsync(function() {
          fileEntry.file(function(file){
            typeImage= file.type;
            $scope.imagelist = typeImage;
            imagetype = typeImage;
          }, function(e){                                              
          }); 
        })

      }

     function halfthru(fileEntry, newName) {
        window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fileSystem2) {
          fileEntry.copyTo(
            fileSystem2,
            newName,
            onCopySuccess,
            fail
          );
        }, fail);   
      }

      // 6
      function onCopySuccess(entry) {  
          $scope.activeSlide = index;
            $scope.modal1.show();
            $scope.$evalAsync($scope.images.push({file: entry.nativeURL, type: $scope.imagelist}));
            imagesModalCount =  $scope.images.length;
            attachedImageCount  = $scope.imagesAttached.length;

          $scope.$on('$destroy', function() {
            $scope.modal1.remove();
          });

      }

      function fail(error) {

      }
    }, function(err) {

    });
  }

要上传到亚马逊的代码

$scope.uploadImage = function(imageURI, fileName) {
    var deferred = $q.defer();
      createCase.getAws().then(function(awsDetails) {
        var policy = awsDetails.policy;
        var signature = awsDetails.signature;
        var key = awsDetails.key;
        var datenow = awsDetails.datenow;
        var s3URI = encodeURI("https://partners-mobile-app.s3.amazonaws.com/"),
        awsKey = key,
        policyBase64 = policy,
        acl = "public-read";  
          var ft = new FileTransfer();
          var options = new FileUploadOptions();
          options.fileKey = "file";
          options.fileName = fileName;
          options.mimeType = "image/jpeg";
          options.chunkedMode = false;
          options.headers = {
            Connection: "close"
          };
          options.params = {
              "key": fileName,
              "AWSAccessKeyId": key,
              "acl": acl,
              "policy": policyBase64,
              "signature": signature,
              "Content-Type": "image/jpeg"
          };


          var f =  ft.upload(imageURI, s3URI,
            function (e) {
              console.log("uploadimage: "+ imageURI)
               console.log("uploads3uri: "+ s3URI)
              console.log("im in upload now")
              $scope.finalimagelist.push(s3URI+fileName);
              if($scope.finalimagelist.length === $scope.images.length){
                console.log("OK SER GO")
                deferred.resolve($scope.finalimagelist);
              }    
            },
            function (e) {
              deferred.reject(e);
            }, 
          options);
        }
基本上,我选择图像库中的图像,然后上传到亚马逊everythg看起来不错,但它只是没有上传图像,我不知道为什么。

我正在使用AngularJS和插件cordova-plugin-file-transfer

我的info.plist

<key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
    </dict>

1 个答案:

答案 0 :(得分:0)

请看一下。对我而言,整个事情听起来像iOS 9上的ATS / Apple Transport Security问题

这将是它的解决方案: https://stackoverflow.com/a/32710127/3671726