1

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

GET/POST example.com/download

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 ?

3
  • Nothing. I don't know how to do this in JavaScript Commented Nov 28, 2016 at 16:07
  • this may be what your after stackoverflow.com/questions/24501358/… Commented Nov 28, 2016 at 16:08
  • Server side is implemented Commented Nov 28, 2016 at 16:08

1 Answer 1

3

To add headers to the request do something like this:

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

Is it a right way to do file download or I should reimplement my server side?
@JuanMendes, jQuery is tagged. I believe it's fine
@adam Ha, I guess the mobile site doesn't show tags
@alexanoid regarding the download itself. It depends on the how the server is sending the file. Take a look at the reference question stackoverflow.com/questions/4545311/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.