2016-05-13 51 views
0

Need to open blob/PDF in IE windowSetting window.location or window.open in AngularJS gives "access is denied" in IE 11相關。在Internet Explorer中以二進制格式打開PDF

我需要打開它來自於我的服務的字節數組一個PDF:

@RequestMapping("/{dokumentId}") 
public ResponseEntity<byte[]> getDoc(@PathVariable Integer docId, HttpServletRequest request, HttpServletResponse response) throws IOException {  
    byte[] contents = // .. get document 

    HttpHeaders headers = new HttpHeaders(); 
    headers.setContentType(MediaType.parseMediaType("application/pdf")); 
    headers.add("Content-Disposition", "inline;filename=file"); 
    headers.add("Cache-Control","private, must-revalidate, post-check=0, pre-check=0, max-age=1"); 
    headers.add("Pragma", "no-cache"); 
    headers.setContentLength(contents.length); 

    return new ResponseEntity<>(contents, headers, HttpStatus.OK); 
} 

這我想在Internet Explorer 11以下JS開:

$scope.openPdf = function(doc){ 
    var winlogicalname = "document_" + doc.docId; 
    var winparams = 'dependent=yes,locationbar=no,scrollbars=yes,menubar=yes,resizable,screenX=50,screenY=50,width=850,height=1050'; 

    var ieEDGE = navigator.userAgent.match(/Edge/g); 
    var ie = navigator.userAgent.match(/.NET/g); // IE 11+ 
    var oldIE = navigator.userAgent.match(/MSIE/g); 

    if(!ie && !oldIE && !ieEDGE){ 
     // Open PDF in new browser window 
     var detailWindow = window.open("", winlogicalname, winparams); 
    } 

    xxxService.getDoc(doc).success(function(data){ 
     //for browser compatibility 
     if (ie || oldIE || ieEDGE) { 
      var blob = new window.Blob([data], { type: 'application/pdf' });  
      window.navigator.msSaveOrOpenBlob(blob); 
     } else { 
      var binaryAsString = ArrayBufferToString(data).replace(/"/g,""); 
      var pdfAsDataUri = "data:application/pdf;base64," + binaryAsString; 
      var htmlText = '<embed width=100% height=100% type="application/pdf" src="'+ pdfAsDataUri + '"></embed>'; 
      detailWindow.document.write(htmlText); 
      detailWindow.document.close(); 
     } 
    }).error(function(data, status, headers, config){ 
     console.log(data, status, headers, config); 
    }); 
}; 

這可以在Chrome和Firefox中使用(在新窗口中打開,直接用二進制構建,與<embed>。但是,這種方式似乎並沒有在IE中工作,因此msSaveOrOpenBlob。但是,當在IE中保存/打開PDF時,我得到Adobe讀者:「Adobe Acrobat Reader DC could not open 'file.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).」我嘗試將PDF拖放到IE中,它顯示「File does not begin with '%PDF-'.」。

關於如何在IE11中直接使用字節數組打開PDF或構造Blob的任何想法?

回答

1

我改變了我的Web服務這樣的:

byte[] contents = // get contents 

response.getOutputStream().write(contents); 
response.setContentType("application/pdf"); 
response.setContentLength(contents.length); 
response.addHeader("Content-Disposition", "inline;filename="+filename); 
response.addHeader("Cache-Control", "private, must-revalidate, post-check=0, pre-check=0, max-age=1"); 
response.addHeader("Pragma", "public"); 

,然後消耗的客戶端有:

Service.getDocument(docId).success(function(data){ 
    var file = new Blob([data], {type: 'application/pdf'}); 
    if(window.navigator.msSaveOrOpenBlob){ 
     window.navigator.msSaveOrOpenBlob(file, name + ".pdf"); 
    } else { 
     var fileURL = URL.createObjectURL(file); 
     $scope.renderPDF(fileURL, detailWindow.document.getElementById('canvas')); 
    } 
} 

其中$scope.renderPDF使用PDFJS呈現在新窗口中的PDF。請記得在$http.get(或post)方法上設置{responseType:'arraybuffer'})

+0

我不明白這個事件是如何工作的?它所要做的就是下載文件,如果它是IE瀏覽器,對不對?它只是一個後備。 –

+0

@JudsonTerrell正確,它只是對於IE的一種回退 –

+0

我找到了一種讓pdfJs在IE中工作的方式 –

相關問題