簡體   English   中英

在新的瀏覽器標簽中打開pdf內容

[英]Open a pdf content in a new browser tab

我有一個GET Ajax請求,它以應用程序/ pdf格式返回給我一個內容。 例如,我的請求返回此類型的字符串:

%PDF-1.4% 4 0 obj <> streamx endstream endobj 6 0 obj <> streamx ]o 0 + e{Q $ R N。 @@- n wB?L $ .JDL | >獝 [ > { =?_ʻ(寒 1`ؚyW G。 XϽo 1 6 Ӳ. 4(?u C 8 j H l f6*k Fa^?e G= J0。 }7L z p H & L?Z

對那些表現不好的特殊字符不好意思:問題就是這樣。 我想從我的請求中獲得此響應,並在另一個瀏覽器選項卡中正確打印此內容。 我編寫了代碼來做到這一點:我使用了Blob對象。 正確打開了“閱讀pdf”頁面。 但是,我只看到一個完全白色的pdf文檔,這表明我的回答並未寫在已創建的文檔上。

在下面,我向您顯示我的請求的代碼:

$.ajax({
    type: "GET",
    url:urll + parameter,
    async: true,
    crossDomain: true,
    accept: {
          pdf: 'application/pdf'
    },
    success:function(response, status, xhr){

        alert("Response: "+response);
        alert("Status: "+status);
        alert("Xhr: "+xhr);

        // check for a filename
        var filename = "";
        var disposition = xhr.getResponseHeader('Content-Disposition');
        if (disposition && disposition.indexOf('attachment') !== -1) {
            var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
            var matches = filenameRegex.exec(disposition);
            if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
        }

        var type = xhr.getResponseHeader('Content-Type');
        var blob = new Blob([response], { type: type });

        if (typeof window.navigator.msSaveBlob !== 'undefined') {
            // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
            window.navigator.msSaveBlob(blob, filename);
        } else {
            var URL = window.URL || window.webkitURL;
            var downloadUrl = URL.createObjectURL(blob);

            if (filename) {
                // use HTML5 a[download] attribute to specify filename
                var a = document.createElement("a");
                // safari doesn't support this yet
                if (typeof a.download === 'undefined') {
                    //window.location = downloadUrl;
                    window.open(downloadUrl, '_blank');
                } else {                        
                    a.href = downloadUrl;
                    a.download = filename;
                    document.body.appendChild(a);
                    a.setAttribute("target","_blank");
                    a.click();
                }
            } else {
                //window.location = downloadUrl;
                window.open(downloadUrl, '_blank');
            }

            setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); // cleanup
        }
    },
    error: function(xhr,status,error){
        alert("error "+xhr+" "+error+" "+status);
    }
});

據我了解,您使用GET並在新標簽中需要一個pdf文件。 o ...為什么不像這樣在新窗口中打開此網址

var fullUrl = urll + parameter;
window.open(fullUrl);

我在使用AngularJS時遇到了類似的問題($ resource和$ http)。 我的解決方案是設置responseType: 'arraybuffer' 這是XMLHttpRequest的一部分(請參閱https://xhr.spec.whatwg.org/#interface-xmlhttprequest ),但jQuery並未公開。

您可以使用以下插件將其添加到jQuery: https : //github.com/henrya/js-jquery/tree/master/BinaryTransport

正如我所說的,這就是在AngularJS中為我工作的方式。 希望它也是您正在尋找的解決方案:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM