0

I wanted to download a file in the ajax success call with out opening in a new tab/window.

File types would contain images(jpg, png) and all other doc extensions like doc, docx, pdf, ppt, pptx etc

Tried options with window.open and it is always opening in a new window.

$.ajax({
    type: "GET",
    url: "url",
    success: function(result) {
       window.open(download_url);
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
Prats
  • 1,745
  • 4
  • 24
  • 28
  • I think you should look here for the answer http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – Sorter Sep 20 '13 at 04:40

2 Answers2

1

change download_url to the appropriate image link, and img.png to the image you want it to be called when being downloaded

$.ajax({
    type: "GET",
    url: "url",
    success: function(result) {
       var a = $("<a>");
       a.attr("href", download_url).attr("download", "img.png").appendTo("body");
       a[0].click();    
       a.remove();
    }
});
Yussuf S
  • 2,022
  • 1
  • 14
  • 12
0

You can make it to open in the same window like this

window.open("www.youraddress.com","_self")

_self here specifies that it should be opened in same window.