Delphi DLL 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

Delphi DLL Example

var
rest: HCkRest;
success: Boolean;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
fileStream2: HCkStream;
strResponseBody: PWideChar;
jsonResponse: HCkJsonObject;
total_count: Integer;
i: Integer;
count_i: Integer;
type: PWideChar;
id: PWideChar;
sequence_id: PWideChar;
etag: PWideChar;
sha1: PWideChar;
name: PWideChar;
description: PWideChar;
size: Integer;
path_collectionTotal_count: Integer;
created_at: PWideChar;
modified_at: PWideChar;
trashed_at: Boolean;
purged_at: Boolean;
content_created_at: PWideChar;
content_modified_at: PWideChar;
created_byType: PWideChar;
created_byId: PWideChar;
created_byName: PWideChar;
created_byLogin: PWideChar;
modified_byType: PWideChar;
modified_byId: PWideChar;
modified_byName: PWideChar;
modified_byLogin: PWideChar;
owned_byType: PWideChar;
owned_byId: PWideChar;
owned_byName: PWideChar;
owned_byLogin: PWideChar;
shared_link: Boolean;
parentType: PWideChar;
parentId: PWideChar;
parentSequence_id: PWideChar;
parentEtag: PWideChar;
parentName: PWideChar;
item_status: PWideChar;
j: Integer;
count_j: Integer;
sequence_id_bool: Boolean;
etag_bool: Boolean;

begin
rest := CkRest_Create();

//  URL: https://upload.box.com/api/2.0/files/content
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'upload.box.com',port,bTls,bAutoReconnect);
if (success <> True) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(CkRest_getConnectFailReason(rest)));
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

CkRest_putPartSelector(rest,'1');
CkRest_AddHeader(rest,'Content-Disposition','form-data; name="attributes"');
CkRest_SetMultipartBodyString(rest,'{"name":"tigers.jpeg", "parent":{"id":"BOX_FOLDER_ID"}}');

CkRest_putPartSelector(rest,'2');
fileStream2 := CkStream_Create();
CkStream_putSourceFile(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_putPartSelector(rest,'0');

CkRest_AddHeader(rest,'Authorization','Bearer BOX_ACCESS_TOKEN');

CkRest_putPartSelector(rest,'0');
CkRest_AddHeader(rest,'Content-Type','multipart/form-data');

strResponseBody := CkRest__fullRequestMultipart(rest,'POST','/api/2.0/files/content');
if (CkRest_getLastMethodSuccess(rest) <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

jsonResponse := CkJsonObject_Create();
CkJsonObject_Load(jsonResponse,strResponseBody);

total_count := CkJsonObject_IntOf(jsonResponse,'total_count');
i := 0;
count_i := CkJsonObject_SizeOfArray(jsonResponse,'entries');
while i < count_i do
  begin
CkJsonObject_putI(jsonResponse,i);
    type := CkJsonObject__stringOf(jsonResponse,'entries[i].type');
    id := CkJsonObject__stringOf(jsonResponse,'entries[i].id');
    sequence_id := CkJsonObject__stringOf(jsonResponse,'entries[i].sequence_id');
    etag := CkJsonObject__stringOf(jsonResponse,'entries[i].etag');
    sha1 := CkJsonObject__stringOf(jsonResponse,'entries[i].sha1');
    name := CkJsonObject__stringOf(jsonResponse,'entries[i].name');
    description := CkJsonObject__stringOf(jsonResponse,'entries[i].description');
    size := CkJsonObject_IntOf(jsonResponse,'entries[i].size');
    path_collectionTotal_count := CkJsonObject_IntOf(jsonResponse,'entries[i].path_collection.total_count');
    created_at := CkJsonObject__stringOf(jsonResponse,'entries[i].created_at');
    modified_at := CkJsonObject__stringOf(jsonResponse,'entries[i].modified_at');
    trashed_at := CkJsonObject_IsNullOf(jsonResponse,'entries[i].trashed_at');
    purged_at := CkJsonObject_IsNullOf(jsonResponse,'entries[i].purged_at');
    content_created_at := CkJsonObject__stringOf(jsonResponse,'entries[i].content_created_at');
    content_modified_at := CkJsonObject__stringOf(jsonResponse,'entries[i].content_modified_at');
    created_byType := CkJsonObject__stringOf(jsonResponse,'entries[i].created_by.type');
    created_byId := CkJsonObject__stringOf(jsonResponse,'entries[i].created_by.id');
    created_byName := CkJsonObject__stringOf(jsonResponse,'entries[i].created_by.name');
    created_byLogin := CkJsonObject__stringOf(jsonResponse,'entries[i].created_by.login');
    modified_byType := CkJsonObject__stringOf(jsonResponse,'entries[i].modified_by.type');
    modified_byId := CkJsonObject__stringOf(jsonResponse,'entries[i].modified_by.id');
    modified_byName := CkJsonObject__stringOf(jsonResponse,'entries[i].modified_by.name');
    modified_byLogin := CkJsonObject__stringOf(jsonResponse,'entries[i].modified_by.login');
    owned_byType := CkJsonObject__stringOf(jsonResponse,'entries[i].owned_by.type');
    owned_byId := CkJsonObject__stringOf(jsonResponse,'entries[i].owned_by.id');
    owned_byName := CkJsonObject__stringOf(jsonResponse,'entries[i].owned_by.name');
    owned_byLogin := CkJsonObject__stringOf(jsonResponse,'entries[i].owned_by.login');
    shared_link := CkJsonObject_IsNullOf(jsonResponse,'entries[i].shared_link');
    parentType := CkJsonObject__stringOf(jsonResponse,'entries[i].parent.type');
    parentId := CkJsonObject__stringOf(jsonResponse,'entries[i].parent.id');
    parentSequence_id := CkJsonObject__stringOf(jsonResponse,'entries[i].parent.sequence_id');
    parentEtag := CkJsonObject__stringOf(jsonResponse,'entries[i].parent.etag');
    parentName := CkJsonObject__stringOf(jsonResponse,'entries[i].parent.name');
    item_status := CkJsonObject__stringOf(jsonResponse,'entries[i].item_status');
    j := 0;
    count_j := CkJsonObject_SizeOfArray(jsonResponse,'entries[i].path_collection.entries');
    while j < count_j do
      begin
CkJsonObject_putJ(jsonResponse,j);
        type := CkJsonObject__stringOf(jsonResponse,'entries[i].path_collection.entries[j].type');
        id := CkJsonObject__stringOf(jsonResponse,'entries[i].path_collection.entries[j].id');
        sequence_id_bool := CkJsonObject_IsNullOf(jsonResponse,'entries[i].path_collection.entries[j].sequence_id');
        etag_bool := CkJsonObject_IsNullOf(jsonResponse,'entries[i].path_collection.entries[j].etag');
        name := CkJsonObject__stringOf(jsonResponse,'entries[i].path_collection.entries[j].name');
        j := j + 1;
      end;

    i := i + 1;
  end;

CkRest_Dispose(rest);
CkStream_Dispose(fileStream2);
CkJsonObject_Dispose(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"
    }
  ]
}