2017-10-20 101 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

類型錯誤:在「URL」無法執行「createObjectURL」 – Donnie