2016-06-26 71 views
0

我试图建立一个应用程序使用Ionic和科尔多瓦,其功能的一部分是上传和下载文件从安全的位置,知道这样的命令正在使用卷曲,而我把我的凭据用户:通过的方式,但在科尔多瓦我使用$ cordovaFileTransfer

所以我的问题是:我如何可以添加我的凭据知道需要验证上传和下载文件。

,这里是我使用的代码:

document.addEventListener('deviceready', function() { 

    var url = "https://serverlink/thing.txt"; 
    var targetPath = cordova.file.externalRootDirectory + "/cloud/thing.txt"; 
    var trustHosts = true; 
    var options = { 
     params: { 
      // Do i put my credentials here ?? 
     } 
    }; 
    console.log('before download'); 
    $cordovaFileTransfer.download(url, targetPath, options, trustHosts) 
     .then(function(result) { 
      // Success! 
      console.log("done downloading the file"); 
     }, function(err) { 
      // Error 
      console.log('error yaw !!', err); 
     }, function(progress) { 
      $timeout(function() { 
       $scope.downloadProgress = (progress.loaded/progress.total) * 100; 
      }); 
     }); 

}, false); 

所以,我测试上面这里我把我的凭据选项对象内部PARAMS的代码,但它不工作。

回答

0

我找到了这个答案,我的资源使用基本验证,所以为了送我需要的任何请求进行身份验证,我管理,通过执行以下操作:

authReader = function(username, password) { 
       var tok = username + ':' + password; 
       var hash = btoa(tok); 
       return "Basic " + hash; 
}; 
var options = { 
    headers : { 
      'Authorization': authHeaderValue('user', 'pass') 
    } 
} 
$cordovaFileTransfer.download(url, targetPath, options, trustHosts) 
       .then(function(result) { 
        // Success! 
        console.log("done downloading the file"); 
       }, function(err) { 
        // Error 
        console.log('error yaw !!', err); 
       }, function(progress) { 
        $timeout(function() { 
         $scope.downloadProgress = (progress.loaded/progress.total) * 100; 
        }); 
}); 

现在我设定的具有基本授权的标题(散列在Base64 中),并直接发送请求并从我的安全服务器取回我的文件。

这基本上是我的问题的解决方案。

相关问题