1

I've seen this question a lot but nothing answered my question (or what i've found).

onClick I point to a url of the thing I click on. <- that bit isn't really an issue.

My HTML looks like:

<a id="img_dwn_gen" href="" style="display: none; visibility: hidden; opacity: 0;"><a/>

Then within my onClick event I attach the attribute to the image and then set a timeOut. As I need to wait a bit to grab the image.

$("#img_dwn_gen").attr("src","www.path/to/image.com");

    setTimeout(function() {
      $("#img_dwn_gen").trigger('click');
    }, 500)

I tried using the download attribute which works but it needs to work on IE9+ which doesn't support the download attribute.

How can I add an image path to a HTML attribute and force the browser to automatically download it regardless of browsers (IE9+ and all latest).

It needs to be done in the front-end JS as well because I can't access the url path anywhere else.

pourmesomecode
  • 4,108
  • 10
  • 46
  • 87
  • 2
    Instead of ```setTimeout()``` it's better to use the ```.on('load')``` handler like shown here: http://stackoverflow.com/questions/4285042/asychronously-load-images-with-jquery – dekkard Mar 23 '16 at 12:08
  • Wait, I may have misunderstood your request. You want to force the user to start a download and save the file? This doesn't work for security reasons. Also in your code you need to change the 'href' attribute, because it is an anchor, not an image. – Siderite Zackwehdex Mar 23 '16 at 12:43
  • @SideriteZackwehdex yeah sorry. Just `onClick` download an image. The `download` attribute works but doesn't work in IE and I can't find a work-around that's working – pourmesomecode Mar 23 '16 at 12:45
  • @dekkard thanks man! yeah, that's probably a better solution then just using a `setTimeout` – pourmesomecode Mar 23 '16 at 12:46
  • The only way I can think of is to change the URL of the page. `document.location=url`, but that means the browser needs to recognize it as a downloadable file, rather than something it can open, and the server there needs to set the mime type correctly as well. I mean, if you just change the location of the document to an image url it will load the image and that's it. Also, changing the location will mess with local page javascript, so you should do it in an iframe or something. So no, I don't think it can be done. – Siderite Zackwehdex Mar 23 '16 at 12:47
  • @SideriteZackwehdex yeah that's the issue i'm having. I grab the url on the cilent-side so I need to force the browser to download it on the client side. I was hoping there was some kind of work around for it :p – pourmesomecode Mar 23 '16 at 12:53

0 Answers0