2017-10-20 10 views
0

ボタンはオンザフライで生成されたPDFをダウンロードする必要があります。 PDFはAPIによって正しく生成されます。クライアント側のコードで処理されたファイルが破損していて、を開けません。Vue2:ダウンロードされたPDFが壊れています

プロセスフロー:

  1. ボタンを
  2. exportDataAsPDFActionをクリックしたがトリガされた
  3. POST要求を構築し
  4. レスポンスを実行している生のPDFファイルが含まれている
  5. ファイルのダウンロードは、下記により開始されますコールバックロジック:

this.exportDataAsPDFAction({ 
    gridKey: this.grid.Key, 
    callback: response => { 
    let hiddenElement = document.createElement('a'); 

    // This seems to be the problem area 
    hiddenElement.href = 'data:application/pdf;base64,' + window.btoa(encodeURIComponent(response)); 
    hiddenElement.target = '_blank'; 
    hiddenElement.download = 'Results.pdf'; 
    hiddenElement.click(); 
    } 
}); 

エクストラ:所望のフォーマット作品としてCSVと同じタスクを実行

hiddenElement.href = 'data:attachment/csv,' + encodeURI(response); 

答えて

0

hiddenElement.href = window.URL.createObjectURL(response);

+0

TypeError例外を試してみてください:そのエンコーディングとファイル名の拡張子がある唯一の違いは、 'URL' の 'createObjectURL' を実行できませんでした。 – Donnie

関連する問題