alexanoid alexanoid - 1 year ago 191
jQuery Question

JavaScript get file from server

I have a following server endpoint that expects X-DOWNLOAD:yes request header parameter:


If X-DOWNLOAD:yes is present server returns file to client. If no - redirects user to another page.

How to make a JavaScript call from client in order to get a file(set X-DOWNLOAD:yes parameter) - show browser Save dialog ?

Answer Source

To add headers to the request do something like this:

    type: 'POST',
    url: url,
    headers: {
    //beforeSend: function(xhr) { 
    //  xhr.setRequestHeader("X-Download, "yes"); 
}).done(function(data) { 

For download take a look at Download a file by jQuery.Ajax