Why does the download attribute not work?

1

I'm trying to hit a link code but I'm not getting the result I'd like, it's not working. How to proceed?

<a href="http://www.w3schools.com/images/myw3schoolsimage.jpg" download="aaaa"> download </a>
    
asked by anonymous 10.03.2016 / 19:25

2 answers

4
  • In Google Chrome it works normally

  • Is not supported by any version of Internet Explorer (not even 11), however it is supported by Microsoft version 13 or higher.

  • Not supported by Safari or iOS

Details on: link

Alternative

If you use a back-end language, you can create a webproxy to download external files, such as PHP (tell me if you use any other language in the backend that I'll try to provide an example):

proxy.php:

<?php
set_time_limit(0);

if (empty($_GET['url']) || preg_match('#^(http|https)://[a-z0-9]#i', $_GET['url']) === 0) {
    echo 'URL inválida';
    exit;
}

$url = $_GET['url'];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);

$nome = basename($url);

header('Content-Disposition: attachment; filename="' . $nome . '"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . strlen($data));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');

echo $data;

And the link like this:

<a href="proxy.php?url=http://www.w3schools.com/images/myw3schoolsimage.jpg">download</a>
    
10.03.2016 / 19:44
3

I think this is incompatible with your browser, as mentioned by @Guilherme, a simple javascript can show if your browser supports this attribute.

alert(("download" in document.createElement("a") ? "Possui suporte." : "Não há suporte."));
    
10.03.2016 / 19:49