2

I have an image that i want to download.

I tried doing this with an iframe and it did not work. I tried doing it with a link with the HTML5 "download" attribute and it worked on chrome but on firefox it opend a new window.

my code:

var href = $("#largeImageContaier img").attr("src")

$("#dlpic").attr("href", href);
document.getElementById("dlpic").click()

html:

<a href="" id="dlpic" download="alternate-filename.png">

I want to download the image directly to the browser like the code above does in chrome but how do i make it do the same for other browsers, in this example firefox

ThunD3eR
  • 3,216
  • 5
  • 50
  • 94

2 Answers2

2

If you're using FireFox, you should be able to use the download attribute

Try actually setting the href to something. After I did that, it worked in both Chrome and FireFox.

<a href="resource.html" download="The-name-I-want-to-use.html">Download</a>

MDN says that download is used for setting the name you want the resource to have when downloaded. You still need to provide a href.

Another thing to note about download according to MDN:

This attribute is only honored for links to resources with the same-origin.


If you want to check for download support without having to use any libraries, you can use:

var dlAttrSupported = (function () {
    return !!("download" in document.createElement("a"));
}());
zero298
  • 25,467
  • 10
  • 75
  • 100
0

From http://webdesign.tutsplus.com/tutorials/quick-tip-using-the-html5-download-attribute--cms-23880

You can use Modernizr to detect if the browser supports it and alternatively display instructions under the link to tell the user to 'right click and save as.'

if ( ! Modernizr.adownload ) {
    var $link = $('a');

    $link.each(function() {
        var $download = $(this).attr('download');

        if (typeof $download !== typeof undefined && $download !== false) {
          var $el = $('<div>').addClass('download-instruction').text('Right-click and select "Download Linked File"');
          $el.insertAfter($(this));
        }

    });
}

Here's a list of which browsers support the HTML5 download attribute: http://caniuse.com/#search=download

Matt
  • 5,315
  • 1
  • 30
  • 57
  • 1
    not really what i am looking for. modernizer might grant me the possibility to detect if the browser supports html5 "download" or not but it does not give me the desiered functionality. is there no crossrowser way? I want to point out that the href is url to another site, – ThunD3eR Feb 09 '16 at 21:49
  • If the browser doesn't support the HTML5 download attribute, you are out of luck. – Matt Feb 09 '16 at 22:14