Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

Seppe S.'s avatar
Seppe S.
New member | Level 2
10 years ago

What is the http code to write file contents

I know how to read the contents of a dropbox file in a js variable, using pure javascript.
It's ajax, without the use of jquery or any dropbox or other library.
Similarly, I would like to create/write/update a dropbox file with data from a js variable without uploading an existing file.
What is the http code for this?

3 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    I just put together a simple example using just XMLHttpRequest:

    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);
  • Seppe S.'s avatar
    Seppe S.
    New member | Level 2
    10 years ago

    This is it! What a delight.
    With just a little bit of code we have a simple filesystem at our disposal for async storage in a local dropbox folder, with all the dropbox goodies on top.
    The feature is browser independent and works from a local as well as a remote hosted script, but the dropbox site must be available online.
    I assume that many developers will be very happy with this feature: Create, Delete, Read and Write folders and files in a local dropbox folder, without the need for code from an external library.

    Note: the "overwrite" tag is needed to replace a file: xhr.setRequestHeader("Dropbox-API-Arg", '{"path": "' + path + '","mode":{".tag":"overwrite"}}');

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!