Why does the download attribute not work?

Asked

Viewed 1,158 times

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>
  • Have you seen if it’s a restriction on your network?

  • Make sure the browser you are testing has HTML5 support

2 answers

4

  • In the Google Chrome it works normally

  • In the Firefox it is necessary that the file in href is from the same origin as the page you are calling.

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

  • Is not supported by Safari or iOS

Details in: http://caniuse.com/#search=download%20attribute

Alternative

If using a back-end language it is possible to create a "webproxy" to download external files, such as with PHP (inform if you use any other language in the back-end that I will 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>
  • Thank you for the strength, I’m using Vb.net with MVC

  • @Fabiomoskfabinho MVC means Asp.net-mvc? Which version?

  • @Fabiomoskfabinho qual versão do Asp.net-mvc?

3

I believe it is incompatibility of 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."));

  • I did that and came to Alert as has Suport in the case of Chrome, thanks by force

Browser other questions tagged

You are not signed in. Login or sign up in order to post.