2017-10-24 13 views
0

編集:REST APIに作られたPOSTリクエストによって返されたCSVファイルをダウンロードする方法を

htmlファイル:コントローラで

<button data-ng-click="$ctrl.toCSV()">ToCSV</button> 

コード:

$scope.toCSV = function() { 
    var sampleJson = {"key1": ["val1", "val2"], "key2": "valKey2", "key3": "valKey3"}; 
    // the service forms and returns the url string 
    var url = vm.originUrl + sampleService.getCSVDetails(id, false, "csv").url; 
    var form = $('<form action="' + url + '" method="post" style="display:none;">' + 
      '<input type="text/json" name="jsonData" value="' + sampleJson + '" />' + '</form>'); 
     $('body').append(form); 
     form.submit(); 
    }; 

私はないですどこに間違っているのか理解できるようになったので、私は多くのソリューションをチェックしました。私は郵便配達を使用して同じことを通過したときに、私は取得していますという応答は次のとおりです。

cache-control →no-store, no-cache=set-cookie 
content-disposition →attachment; filename=SampleDetails.csv 
content-encoding →gzip 
content-language →en-US 
content-length →767 
content-type →application/octet-stream 
date →Wed, 25 Oct 2017 15:13:51 GMT 
expires →Thu, 01 Dec 1994 16:00:00 GMT 
pragma →no-cache 
x-frame-options →SAMEORIGIN 
x-powered-by →Servlet/3.1 

は、私はちょうどそれは、テキスト「ToCSV」をクリックの上、ブラウザにダウンロードされるように、これを設定するかどうかはわかりません。今はダウンロードの代わりに新しい空のページで開くだけですが、郵便配達員はファイルの内容を取得できます。

jsonデータをバンドルし、postメソッドを使用してURLをヒットする方法を教えてください。

ありがとうございます!

+0

私はwindow.locationをPOSTリクエストを使用して使用しようとしている可能性があります...私はそれが動作するとは思わない...任意の回避策ですか? – user3868051

答えて

1

私はfile-saverを使用して私のpdfファイルをダウンロードしました。

function downloadPdf(id) { 
    myServiceToGetFile(id).then(function(response) { 
     var blob = new Blob([response.data], { type: "application/octet-stream"}); 
     //change download.pdf to the name of whatever you want your file to be 
     fileSaver.saveAs(blob, 'fileName.pdf'); 
    } 
} 

とサービスは次のようになります。

function myServiceToGetFile(id) { 
    return $http.get(
     baseUrl + '/link/to/pdf/' + id, 
     {responseType: "arraybuffer"}); // must add this to work 
} 

PS:私たちはいくつかの理由は縮小の問題により、サービス内のライブラリを入れていました。

+0

こんにちは@Denisa Lavinia Profir残念なことに私はここにサードパーティの図書館を使用するはずだとは思わないが、感謝..私は最後の手段としてこのオプションを残しています:) – user3868051

+0

フォームや必要なURLにリダイレクトする他のもの..データと一緒にwindows.locationの代わりに? – user3868051

+0

コンテンツをサービスとして追加できます。 –

関連する問題