user2161522
user2161522

Reputation: 203

Is there a way to download a file using browser from RESTful webservice which asks for Authorization header

I am currently trying to download the file using ajax request from my REST based servie using browser as.

$('#button').click(function (url)
{
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.setRequestHeader('Authorization', "Bearer " + token);
    xhr.onload = function(e) {
        alert('success')
        //what should I do here to get file downloaded using browser
    };
    xhr.send();
}););

Upvotes: 0

Views: 90

Answers (1)

chirag
chirag

Reputation: 1828

you can use below code.

$('#button').click(function (url)
{
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.setRequestHeader('Authorization', "Bearer " + token);
    xhr.onload = function(e) {
        alert('success')
        var file=e.target.files[0]; //you can get file this way.
    };
    xhr.send();
}););

Upvotes: 1

Related Questions