0

There is a way to download a file (instead of opening it) with HTML:

<a href="./images/myimage.png" download="myimage.png">Download</a>

What I am looking for is a way to do this with JavaScript. What I have is an AJAX call to retrieve a URL for an image:

function followUpFunction(){
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
        var status = (xmlHttp.status == 200) ? "good" : "bad";
        var returnedImageURL = xmlHttp.responseText;
        window.open(returnedImageURL);
        return;                                                                                                                           
    }                                                                                                                                     
}

You can see that way I'm downloading it now is by using JavaScript's window.open function.

The way it works now, the browser gives a pop-up window warning, which people don't like or don't notice, this ends up with many people not getting their download. I would prefer the image just download immediately like it does with my HTML snippet above. Also, I don't want the current window replaced or written over, I want it to remain visible just as it is.

So, is there a way I can do this with JavaScript?

Thanks!

Brian
  • 1,726
  • 2
  • 24
  • 62
  • Depending on browser support [FileSaver.js](https://github.com/eligrey/FileSaver.js/) is an option – Patrick Barr Jul 07 '17 at 17:19
  • @t.niese I don't want to use jQuery. – Brian Jul 07 '17 at 17:21
  • @PatrickBarr I don't want to use external libraries. – Brian Jul 07 '17 at 17:22
  • 3
    This [answer](https://stackoverflow.com/a/37673039/1960455) to the linked question does not use jQuery. – t.niese Jul 07 '17 at 17:22
  • @t.niese You are correct, I saw "js or query" and my mind smooshed them together to make "jquery". That solution worked perfectly, thank you! I had thought about doing it that way, but I didn't know how it would get clicked. I had no idea there is a "click" method; now I do. Thanks again. Do you want to post an answer so I can mark it as the solution? – Brian Jul 07 '17 at 17:49
  • @Brian it is already answered in the linked question so there is no need to create a new answer that would not provide any additional information. – t.niese Jul 07 '17 at 18:51
  • @t.niese Gotcha ... thanks again. – Brian Jul 08 '17 at 02:30

0 Answers0