Javascript 如何使用浏览器打开PDF Blob';是下载而不是下载?

Javascript 如何使用浏览器打开PDF Blob';是下载而不是下载?,javascript,pdf,Javascript,Pdf,我有一个PDF文件的ArrayBuffer,我想使用浏览器的内置PDF查看器打开它 以下是我正在使用的代码: const blob = new Blob([pdfBuffer], { type: 'application/pdf' }); const a = document.createElement('a'); a.href = window.URL.createObjectURL(blob); a.download = fileName; a.style.position = 'fixed

我有一个PDF文件的
ArrayBuffer
,我想使用浏览器的内置PDF查看器打开它

以下是我正在使用的代码:

const blob = new Blob([pdfBuffer], { type: 'application/pdf' });
const a = document.createElement('a');
a.href = window.URL.createObjectURL(blob);
a.download = fileName;
a.style.position = 'fixed';
a.target = '_blank';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
这始终会导致PDF文件下载到设备上的下载文件夹,而不是在浏览器中使用PDF查看器打开新选项卡。我已经在Chrome、Opera和Firefox上测试过了,结果也一样


以前,当PDF实际上是通过在响应中设置
Content Disposition
标题从服务器本身下载时,我已经这样做了,但我不确定如何使用这样的客户端
Blob

您可以对
window.URL.createObjectURL(Blob)的结果使用
window.open()
在新窗口中打开PDF,而不是下载。例如:

var blob = new Blob([pdfBuffer], {type: 'application/pdf'});
var blobURL = URL.createObjectURL(blob);
window.open(blobURL);

通过与pdfMake库的结合,可以很好地实现这一点

//Preview PDF
export const previewDocument = (content) => {
  const pdfDocGenerator = pdfMake.createPdf(content);
  // Get PDF blob and open in new window
  pdfDocGenerator.getBlob((blob) => {
    let blobURL = URL.createObjectURL(blob);
    window.open(blobURL);
  });
}

您是否尝试将createObjectURL返回的字符串传递到window.open()?很好,谢谢。看起来我可以删除
a.download=fileName我的代码的一部分。