2016-06-26 9 views
0

IonicとCordovaを使用してアプリケーションを構築しようとしていますが、その機能の一部は安全な場所からファイルをアップロードしてダウンロードし、 ユーザー:pass方法で、私の資格を入れている間にカールを使って作業していますが、私は$ 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; 
        }); 
}); 

を今では私が設定ヘッダーを基本認証(Baseでハッシュ)し、要求を直接送信して、セキュリティで保護されたサーバーからファイルを取り戻しました。

これは基本的に私の質問のための解決策でした。

関連する問題