Tcl 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

Tcl Example


load ./chilkat.dll

set rest [new_CkRest]

#  URL: https://upload.box.com/api/2.0/files/content
set bTls 1
set port 443
set bAutoReconnect 1
set success [CkRest_Connect $rest "upload.box.com" $port $bTls $bAutoReconnect]
if {[expr $success != 1]} then {
    puts "ConnectFailReason: [CkRest_ConnectFailReason $rest]"
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    exit
}

CkRest_put_PartSelector $rest "1"
CkRest_AddHeader $rest "Content-Disposition" "form-data; name=\"attributes\""
CkRest_SetMultipartBodyString $rest "{\"name\":\"tigers.jpeg\", \"parent\":{\"id\":\"BOX_FOLDER_ID\"}}"

CkRest_put_PartSelector $rest "2"
set fileStream2 [new_CkStream]

CkStream_put_SourceFile $fileStream2 "tigers.jpeg"
CkRest_AddHeader $rest "Content-Disposition" "form-data; name=\"tigers.jpeg\"; filename=\"tigers.jpeg\""
CkRest_AddHeader $rest "Content-Type" "image/jpeg"
CkRest_SetMultipartBodyStream $rest $fileStream2

CkRest_put_PartSelector $rest "0"

CkRest_AddHeader $rest "Authorization" "Bearer BOX_ACCESS_TOKEN"

CkRest_put_PartSelector $rest "0"
CkRest_AddHeader $rest "Content-Type" "multipart/form-data"

set strResponseBody [CkRest_fullRequestMultipart $rest "POST" "/api/2.0/files/content"]
if {[expr [CkRest_LastMethodSuccess $rest] != 1]} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkStream $fileStream2
    exit
}

set jsonResponse [new_CkJsonObject]

CkJsonObject_Load $jsonResponse $strResponseBody

set total_count [CkJsonObject_IntOf $jsonResponse "total_count"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jsonResponse "entries"]
while {[expr $i < $count_i]} {
    CkJsonObject_put_I $jsonResponse $i
    set type [CkJsonObject_stringOf $jsonResponse "entries[i].type"]
    set id [CkJsonObject_stringOf $jsonResponse "entries[i].id"]
    set sequence_id [CkJsonObject_stringOf $jsonResponse "entries[i].sequence_id"]
    set etag [CkJsonObject_stringOf $jsonResponse "entries[i].etag"]
    set sha1 [CkJsonObject_stringOf $jsonResponse "entries[i].sha1"]
    set name [CkJsonObject_stringOf $jsonResponse "entries[i].name"]
    set description [CkJsonObject_stringOf $jsonResponse "entries[i].description"]
    set size [CkJsonObject_IntOf $jsonResponse "entries[i].size"]
    set path_collectionTotal_count [CkJsonObject_IntOf $jsonResponse "entries[i].path_collection.total_count"]
    set created_at [CkJsonObject_stringOf $jsonResponse "entries[i].created_at"]
    set modified_at [CkJsonObject_stringOf $jsonResponse "entries[i].modified_at"]
    set trashed_at [CkJsonObject_IsNullOf $jsonResponse "entries[i].trashed_at"]
    set purged_at [CkJsonObject_IsNullOf $jsonResponse "entries[i].purged_at"]
    set content_created_at [CkJsonObject_stringOf $jsonResponse "entries[i].content_created_at"]
    set content_modified_at [CkJsonObject_stringOf $jsonResponse "entries[i].content_modified_at"]
    set created_byType [CkJsonObject_stringOf $jsonResponse "entries[i].created_by.type"]
    set created_byId [CkJsonObject_stringOf $jsonResponse "entries[i].created_by.id"]
    set created_byName [CkJsonObject_stringOf $jsonResponse "entries[i].created_by.name"]
    set created_byLogin [CkJsonObject_stringOf $jsonResponse "entries[i].created_by.login"]
    set modified_byType [CkJsonObject_stringOf $jsonResponse "entries[i].modified_by.type"]
    set modified_byId [CkJsonObject_stringOf $jsonResponse "entries[i].modified_by.id"]
    set modified_byName [CkJsonObject_stringOf $jsonResponse "entries[i].modified_by.name"]
    set modified_byLogin [CkJsonObject_stringOf $jsonResponse "entries[i].modified_by.login"]
    set owned_byType [CkJsonObject_stringOf $jsonResponse "entries[i].owned_by.type"]
    set owned_byId [CkJsonObject_stringOf $jsonResponse "entries[i].owned_by.id"]
    set owned_byName [CkJsonObject_stringOf $jsonResponse "entries[i].owned_by.name"]
    set owned_byLogin [CkJsonObject_stringOf $jsonResponse "entries[i].owned_by.login"]
    set shared_link [CkJsonObject_IsNullOf $jsonResponse "entries[i].shared_link"]
    set parentType [CkJsonObject_stringOf $jsonResponse "entries[i].parent.type"]
    set parentId [CkJsonObject_stringOf $jsonResponse "entries[i].parent.id"]
    set parentSequence_id [CkJsonObject_stringOf $jsonResponse "entries[i].parent.sequence_id"]
    set parentEtag [CkJsonObject_stringOf $jsonResponse "entries[i].parent.etag"]
    set parentName [CkJsonObject_stringOf $jsonResponse "entries[i].parent.name"]
    set item_status [CkJsonObject_stringOf $jsonResponse "entries[i].item_status"]
    set j 0
    set count_j [CkJsonObject_SizeOfArray $jsonResponse "entries[i].path_collection.entries"]
    while {[expr $j < $count_j]} {
        CkJsonObject_put_J $jsonResponse $j
        set type [CkJsonObject_stringOf $jsonResponse "entries[i].path_collection.entries[j].type"]
        set id [CkJsonObject_stringOf $jsonResponse "entries[i].path_collection.entries[j].id"]
        set sequence_id_bool [CkJsonObject_IsNullOf $jsonResponse "entries[i].path_collection.entries[j].sequence_id"]
        set etag_bool [CkJsonObject_IsNullOf $jsonResponse "entries[i].path_collection.entries[j].etag"]
        set name [CkJsonObject_stringOf $jsonResponse "entries[i].path_collection.entries[j].name"]
        set j [expr $j + 1]
    }
    set i [expr $i + 1]
}

delete_CkRest $rest
delete_CkStream $fileStream2
delete_CkJsonObject $jsonResponse

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"
    }
  ]
}