简体   繁体   中英

Opening a html link to read a PDF file on browser is blocked by browser

I can't open a PDF at a tab in a browser from a html link.

<a href="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" target="_blank">PDF</a>

Problem:

在此处输入图片说明

But, if I open the link directly on the browser ( https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf ), that will open in the browser without any issue.

How to solve this?

Fiddle Demo

You can get around it with a proxy script (on the same domain):

PHP example

Save code below as test.php , and start server with php -S localhost:1234 test.php . Go to http://localhost:1234 .

if(isset($_GET['url']) && strpos($_GET['url'],"http") > -1) {
    header('Content-type: application/pdf');
    echo file_get_contents($_GET['url']);
    exit;
}
?>

<a href="?url=https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" target="_blank">PDF</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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