Node.js Box: Upload File

Back to Index

Uploads a file to a destination (parent) folder. The BOX_FOLDER_ID should be replaced with the id of the folder, or 0 for the root folder.

Documentation: https://developer.box.com/reference#upload-a-file

CURL Command

curl https://upload.box.com/api/2.0/files/content \
  -H "Authorization: Bearer BOX_ACCESS_TOKEN" -X POST \
  -F attributes='{"name":"tigers.jpeg", "parent":{"id":"BOX_FOLDER_ID"}}' \
  -F file=@tigers.jpeg

Node.js Example

var os = require('os');
if (os.platform() == 'win32') {  
    var chilkat = require('chilkat_node6_win32'); 
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('chilkat_node6_arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('chilkat_node6_linux32');
    } else {
        var chilkat = require('chilkat_node6_linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('chilkat_node6_macosx');
}

function chilkatExample() {

    var rest = new chilkat.Rest();
    var success;

    //  URL: https://upload.box.com/api/2.0/files/content
    var bTls = true;
    var port = 443;
    var bAutoReconnect = true;
    success = rest.Connect("upload.box.com",port,bTls,bAutoReconnect);
    if (success !== true) {
        console.log("ConnectFailReason: " + rest.ConnectFailReason);
        console.log(rest.LastErrorText);
        return;
    }

    rest.PartSelector = "1";
    rest.AddHeader("Content-Disposition","form-data; name=\"attributes\"");
    rest.SetMultipartBodyString("{\"name\":\"tigers.jpeg\", \"parent\":{\"id\":\"BOX_FOLDER_ID\"}}");

    rest.PartSelector = "2";
    var fileStream2 = new chilkat.Stream();
    fileStream2.SourceFile = "tigers.jpeg";
    rest.AddHeader("Content-Disposition","form-data; name=\"tigers.jpeg\"; filename=\"tigers.jpeg\"");
    rest.AddHeader("Content-Type","image/jpeg");
    rest.SetMultipartBodyStream(fileStream2);

    rest.PartSelector = "0";

    rest.AddHeader("Authorization","Bearer BOX_ACCESS_TOKEN");

    rest.PartSelector = "0";
    rest.AddHeader("Content-Type","multipart/form-data");

    var strResponseBody = rest.FullRequestMultipart("POST","/api/2.0/files/content");
    if (rest.LastMethodSuccess !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    var jsonResponse = new chilkat.JsonObject();
    jsonResponse.Load(strResponseBody);

    var total_count;
    var i;
    var count_i;
    var type;
    var id;
    var sequence_id;
    var etag;
    var sha1;
    var name;
    var description;
    var size;
    var path_collectionTotal_count;
    var created_at;
    var modified_at;
    var trashed_at;
    var purged_at;
    var content_created_at;
    var content_modified_at;
    var created_byType;
    var created_byId;
    var created_byName;
    var created_byLogin;
    var modified_byType;
    var modified_byId;
    var modified_byName;
    var modified_byLogin;
    var owned_byType;
    var owned_byId;
    var owned_byName;
    var owned_byLogin;
    var shared_link;
    var parentType;
    var parentId;
    var parentSequence_id;
    var parentEtag;
    var parentName;
    var item_status;
    var j;
    var count_j;
    var sequence_id_bool;
    var etag_bool;

    total_count = jsonResponse.IntOf("total_count");
    i = 0;
    count_i = jsonResponse.SizeOfArray("entries");
    while (i < count_i) {
        jsonResponse.I = i;
        type = jsonResponse.StringOf("entries[i].type");
        id = jsonResponse.StringOf("entries[i].id");
        sequence_id = jsonResponse.StringOf("entries[i].sequence_id");
        etag = jsonResponse.StringOf("entries[i].etag");
        sha1 = jsonResponse.StringOf("entries[i].sha1");
        name = jsonResponse.StringOf("entries[i].name");
        description = jsonResponse.StringOf("entries[i].description");
        size = jsonResponse.IntOf("entries[i].size");
        path_collectionTotal_count = jsonResponse.IntOf("entries[i].path_collection.total_count");
        created_at = jsonResponse.StringOf("entries[i].created_at");
        modified_at = jsonResponse.StringOf("entries[i].modified_at");
        trashed_at = jsonResponse.IsNullOf("entries[i].trashed_at");
        purged_at = jsonResponse.IsNullOf("entries[i].purged_at");
        content_created_at = jsonResponse.StringOf("entries[i].content_created_at");
        content_modified_at = jsonResponse.StringOf("entries[i].content_modified_at");
        created_byType = jsonResponse.StringOf("entries[i].created_by.type");
        created_byId = jsonResponse.StringOf("entries[i].created_by.id");
        created_byName = jsonResponse.StringOf("entries[i].created_by.name");
        created_byLogin = jsonResponse.StringOf("entries[i].created_by.login");
        modified_byType = jsonResponse.StringOf("entries[i].modified_by.type");
        modified_byId = jsonResponse.StringOf("entries[i].modified_by.id");
        modified_byName = jsonResponse.StringOf("entries[i].modified_by.name");
        modified_byLogin = jsonResponse.StringOf("entries[i].modified_by.login");
        owned_byType = jsonResponse.StringOf("entries[i].owned_by.type");
        owned_byId = jsonResponse.StringOf("entries[i].owned_by.id");
        owned_byName = jsonResponse.StringOf("entries[i].owned_by.name");
        owned_byLogin = jsonResponse.StringOf("entries[i].owned_by.login");
        shared_link = jsonResponse.IsNullOf("entries[i].shared_link");
        parentType = jsonResponse.StringOf("entries[i].parent.type");
        parentId = jsonResponse.StringOf("entries[i].parent.id");
        parentSequence_id = jsonResponse.StringOf("entries[i].parent.sequence_id");
        parentEtag = jsonResponse.StringOf("entries[i].parent.etag");
        parentName = jsonResponse.StringOf("entries[i].parent.name");
        item_status = jsonResponse.StringOf("entries[i].item_status");
        j = 0;
        count_j = jsonResponse.SizeOfArray("entries[i].path_collection.entries");
        while (j < count_j) {
            jsonResponse.J = j;
            type = jsonResponse.StringOf("entries[i].path_collection.entries[j].type");
            id = jsonResponse.StringOf("entries[i].path_collection.entries[j].id");
            sequence_id_bool = jsonResponse.IsNullOf("entries[i].path_collection.entries[j].sequence_id");
            etag_bool = jsonResponse.IsNullOf("entries[i].path_collection.entries[j].etag");
            name = jsonResponse.StringOf("entries[i].path_collection.entries[j].name");
            j = j+1;
        }

        i = i+1;
    }


}

chilkatExample();

Sample JSON Response Body

{
  "total_count": 1,
  "entries": [
    {
      "type": "file",
      "id": "5000948880",
      "sequence_id": "3",
      "etag": "3",
      "sha1": "134b65991ed521fcfe4724b7d814ab8ded5185dc",
      "name": "tigers.jpeg",
      "description": "a picture of tigers",
      "size": 629644,
      "path_collection": {
        "total_count": 2,
        "entries": [
          {
            "type": "folder",
            "id": "0",
            "sequence_id": null,
            "etag": null,
            "name": "All Files"
          },
          {
            "type": "folder",
            "id": "11446498",
            "sequence_id": "1",
            "etag": "1",
            "name": "Pictures"
          }
        ]
      },
      "created_at": "2012-12-12T10:55:30-08:00",
      "modified_at": "2012-12-12T11:04:26-08:00",
      "trashed_at": null,
      "purged_at": null,
      "content_created_at": "2013-02-04T16:57:52-08:00",
      "content_modified_at": "2013-02-04T16:57:52-08:00",
      "created_by": {
        "type": "user",
        "id": "17738362",
        "name": "sean rose",
        "login": "sean@box.com"
      },
      "modified_by": {
        "type": "user",
        "id": "17738362",
        "name": "sean rose",
        "login": "sean@box.com"
      },
      "owned_by": {
        "type": "user",
        "id": "17738362",
        "name": "sean rose",
        "login": "sean@box.com"
      },
      "shared_link": null,
      "parent": {
        "type": "folder",
        "id": "11446498",
        "sequence_id": "1",
        "etag": "1",
        "name": "Pictures"
      },
      "item_status": "active"
    }
  ]
}