2016-06-26 75 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 中),並直接發送請求並從我的安全服務器取回我的文件。

這基本上是我的問題的解決方案。

相關問題