0

On the click of a button I make an ajax call , a csv-file is created, and a link to the file is returned. On success, I want the file to download.

If on success I do:

 window.open(output);

a new window opens and I can see the correct path to the file in the adress bar. But I get a 404 error, so it is interpreted as a page rather than a file download.

With html links you can use "download" to achieve a file download:

<a href="/path.jpg" download>

Is there some equivalent command in javascript to achieve this download?

------------------------Edit:-----------------------------

It turned out that

window.open(output);

works fine! I used the file system path instead of the URL - I needed to use the url. Now when I click the button, the file downloads at the ajax success method.

(if this question is not unique I can delete it if you think so)

Galivan
  • 4,842
  • 9
  • 44
  • 76
  • 2
    even with the new window, it should not return a 404. Your server doesn't know how the client is handling the file and would send it regardless what's happening on the other side. A 404 always indicates a wrong URL. – Axel Amthor Nov 23 '15 at 09:49
  • Possible duplicate of [Download File Using Javascript/jQuery](http://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery) – r2_d2 Nov 23 '15 at 09:53
  • @Axel Amthor yes, I see that you are right, the URL seems to be the file system path but needs to be the url...I will try it. – Galivan Nov 23 '15 at 09:58

0 Answers0