2017-05-26 376 views
1

這裏是我的情況,我有一個服務器上運行一個PDF生成器,當我做了一些PARAMS的請求,它會給我帶回一個PDF文件時,PDF沒有存儲在它是在運行時生成的服務器。Chrome PDF查看器無法下載文件

一切順利,我可以得到PDF在Chrome的PDF查看器打開,但如果要下載的文件,出現了錯誤,如圖像顯示。 enter image description here

由於Chrome轉到原始URL請求文件,但該文件不是服務器上的靜態資源。

,如果有人遇到了這個問題,我不知道嗎?

+0

我們遇到同樣的問題...而且還沒有解決方案。 – flob

+0

您是否嘗試過在此處建議的解決方案:https://stackoverflow.com/a/21732039/510711? – flob

+0

@flob我已經在Google chrome的小組中發佈了這個消息,據說通過pdf的原始URL下載PDF是Chrome內置PDF查看器的默認行爲。現在我們的解決方案是使用'PDF.js'作爲我們的服務器中構建的默認PDF查看器。欲瞭解更多信息,你可以檢查'PDF.js'github頁面。 –

回答

0

每當你離開用於創建對象URL(window.URL.createObjectURL(...))是非常反對將得到收集垃圾網站。所以你需要保持對該對象的引用。

這適用於我們在Chrome,火狐,Safari瀏覽器,iOS的Safari瀏覽器&安卓先在兼容的瀏覽器在新標籤後顯示PDF 並允許下載(在IE中它只是開始下載):

function openPdfInNewTab(url, 
         postData, 
         description = 'Document', 
         filename = description + '.pdf') { 
    if (!window.navigator.msSaveOrOpenBlob) { 
    var tabWindow = window.open('', '_blank'); 
    var a = tabWindow.document.createElement('a'); 
    a.textContent = 'Loading ' + description + '..'; 
    tabWindow.document.body.appendChild(a); 
    tabWindow.document.body.style.cursor = 'wait'; 
    } else { 
    spinnerService.show('html5spinner'); 
    } 

    $http.post(url, postData, {responseType: 'arraybuffer'}) 
    .then(function showDocument(response) { 
     var file = new Blob([response.data], {type: 'application/pdf'}); 
     if (window.navigator.msSaveOrOpenBlob) { 
     spinnerService.hide('html5spinner'); 
     window.navigator.msSaveOrOpenBlob(file, filename); 
     } else { 
     tabWindow.document.body.style.cursor = 'auto'; 
     var url = a.href = window.URL.createObjectURL(file); 
     a.click(); 
     a.download = filename; 
     } 
     $timeout(function revokeUrl() { 
     window.URL.revokeObjectURL(url); 
     }, 3600000); 
    }, handleDownloadError); 
} 

我們已經在一個新的瀏覽器選項卡中打開PDF文件,並有類似的問題。

對於我們來說,開始的時候我們使用window.URL.createObjectURL代替tabWindow.URL.createObject其顯示的PDF,但並沒有讓下載的再次合作。