Dropbox API Uploading a file from text via XMLHttpRequest in JavaScript


Example

var path = "/test_javascript_upload.txt";
var content = "data to upload";
var accessToken = "<ACCESS_TOKEN>";
var uploadUrl = "https://content.dropboxapi.com/2/files/upload"
var result;

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
        result = xhr.responseText;
        console.log(result);
    }
};
xhr.open("POST", uploadUrl, true);
xhr.setRequestHeader("Authorization", "Bearer " + accessToken);
xhr.setRequestHeader("Content-type", "application/octet-stream");
xhr.setRequestHeader("Dropbox-API-Arg", '{"path": "' + path + '"}');
xhr.send(content);

<ACCESS_TOKEN> should be replaced with the OAuth 2 access token.