强制浏览器下载<a> tag html5

时间:2018-03-23 14:44:02

标签: html html5

How to force mobile browser to download a .jpg file on click from another website. so far i tried:

<a href="https://anotherwebsite.com/example.jpg" target="_blank" download>Click To Download</a>

I found two workarounds for this problem (using javascript or php). I use php to bypass the problem. I create a file named download.php with this code in it:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    if (!is_null($_GET['link']) && !empty($_GET['link'])) {
        $file = $_GET['link'];
        try {
            header('Content-type: image/*');
            header("Content-Disposition: attachment; filename=\"$file\"");
            readfile($file);
        }
        catch (Exception $ex){
            echo $ex->getMessage();
        }
    }
}

then I change the download links to this:

<a href="download.php?link=https://anotherwebsite.com/example.jpg">Click To Download</a>

is there another way that I'm not aware of?

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

根据Anchor元素,此属性仅适用于同源URL。但您仍然可以使用javascript和php来解决此问题。