简体   繁体   English

如何设置内容类型以使用javascript在新选项卡中打开pdf

[英]How to set content type to open pdf in new tab using javascript

I am using javascript. 我正在使用javascript。 I have a pdf file stored on server which i need to open in new tab using javascript. 我在服务器上存储了一个pdf文件,我需要使用javascript在新标签页中打开。 I have tried the below code 我已经尝试了以下代码

function openPDF() {
  var urlToPdfFile = "http://www.demosite.com/pdf/demo.pdf"; //something like this
  window.open(urlToPdfFile);
}

But I keep getting error like below 但我不断收到如下错误

Unknown content type requested.

URL: '/pdf/demo.pdf'.

Requested extension: '.pdf'.

This because I see the content-type is html in the request. 这是因为我在请求中看到content-type是html。

how can I open PDF in new tab using javascript, by providing the content-type...? 如何通过提供内容类型...使用javascript在新标签中打开PDF?

Thanks 谢谢

Try this code, it works: 试试下面的代码,它可以工作:

function openPDF(urlToPdfFile) {
  window.open(urlToPdfFile, 'pdf');
}

openPDF('http://www.demosite.com/pdf/demo.pdf');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM