16

I would like to start a simple file download through the browser, however an access token must be passed with a custom HTTP header:

GET https://my.site.com/some/file
Authorization: access_token

How can I inject the Authorization: header following the site URL? I know it's possible to do that using query string, but I want to do it using headers.

I'm familiar with XMLHttpRequest, but as far as I understand it does not trigger download, it only reads content and the file I want to download is few hundred MBs at least.

xhr.setRequestHeader('Authorization', 'access_token');

This looks like a simple task, but I'm inexperienced coder so any help would be nice. Thanks.

2
  • Why not do it on the serverside, where you'd normally set the headers ? Commented May 16, 2013 at 16:27
  • Because file I want to download is not on my server. My server only generates token. Also, I do not want to use any PHP relaying since this would cost me a lot of traffic. Commented May 16, 2013 at 17:21

3 Answers 3

9

I think this solves your problem:

function toBinaryString(data) {
    var ret = [];
    var len = data.length;
    var byte;
    for (var i = 0; i < len; i++) { 
        byte=( data.charCodeAt(i) & 0xFF )>>> 0;
        ret.push( String.fromCharCode(byte) );
    }

    return ret.join('');
}


var xhr = new XMLHttpRequest;

xhr.open( "GET", "https://my.site.com/some/file" );     

xhr.addEventListener( "load", function(){
    var data = toBinaryString(this.responseText);
    data = "data:application/text;base64,"+btoa(data);
    document.location = data;
}, false);

xhr.setRequestHeader("Authorization", "access_token" );
xhr.overrideMimeType( "application/octet-stream; charset=x-user-defined;" );
xhr.send(null);

Modified answer https://stackoverflow.com/a/10518190/2767026 to fit your needs.

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

3 Comments

Is it possible to put a custom filename for the download?
@Oleksii I dont know, answered a long time ago and at the time this code fit my needs... :( I'm sorry.
Actually it is. As so: link.attr({ "href": data, "download": name + new Date().valueOf() + ext })
0

It is not possible to add custom http header when you download a file by clicking on a link.

However, in your use case, you might store the token in a cookie, which will be automatically added to all browser requests.

Comments

-1

https://stackoverflow.com/a/12372670/1961561 could be a solution for your problem.

$.ajax({
  url: "/test",
  headers: {"X-Test-Header": "test-value"}
});

3 Comments

Thanks, it look like what i want, but i don't use jQuery. Is the a way to do this in "pure javascript"?
Nop, it still does not trigger download.
You can't use ajax to download and save a file to disk for security reasons

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.