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

Posted

技术标签:

【中文标题】var ft = new File Transfer() 在 iOS 模拟器中不起作用。 Cordova FileTransfer 插件问题【英文标题】:var ft = new FileTransfer() doesnt work in iOS simulator. CordovaFileTransfer plugin issue 【发布时间】:2015-11-06 08:53:07 【问题描述】:

我想将我在 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);
        

所以基本上,我从图片库中选择图片,然后上传到亚马逊,一切看起来都不错,但它只是不上传图片,我不知道为什么。

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

我的 info.plist

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

【问题讨论】:

【参考方案1】:

请看看这个。这整件事在我看来像是 iOS 9 上的 ATS / Apple Transport Security 的问题

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

【讨论】:

我已经在我的 plist 文件中添加了 NSTransportSecurity。所以我不认为这个问题 请分享您的info.plist 以及您的白名单设置 NSAppTransportSecurityNSAllowsArbitraryLoads 不认为我每次在 xcode 上运行 cordova 插件白名单时都有将 bcos 列入白名单的设置,它一直告诉我它已被弃用

以上是关于var ft = new File Transfer() 在 iOS 模拟器中不起作用。 Cordova FileTransfer 插件问题的主要内容,如果未能解决你的问题,请参考以下文章

粗体效果对比:(Times New Roman自带) VS (FreeType粗体)

粗体效果对比:(Times New Roman自带) VS (FreeType粗体)

java ftp批量下载异常

AS3 - 对文件和目录的操作

js中的var a = new A;与var a = new A()的区别

Hololens创建读取删除本地文件