Twinone
Twinone

Reputation: 3029

How to download a file over HTTP with AJAX?

I want to download a file from a server. The server part is working correctly.

Im using the following code:

$("#download-button").click(function() {
    alert("starting download");
    $.ajax({
        url:'download/myfile.txt'
    });
});

I get the alert (of course), but not the download.

However, when I manually go to http://srv/download/myfile.txt, i get the file downloaded.

What am I doing wrong in the AJAX call?

Upvotes: 1

Views: 120

Answers (1)

What have you tried
What have you tried

Reputation: 11148

Ajax should not be used to download files. Use instead document.location

// Set the value of the location object 
document.location = 'download/myfile.txt';

Upvotes: 1

Related Questions