PowerBuilder 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

PowerBuilder Example

integer li_rc
oleobject loo_Rest
integer li_Success
integer li_BTls
integer li_Port
integer li_BAutoReconnect
oleobject loo_FileStream2
string ls_StrResponseBody
oleobject loo_JsonResponse
integer li_Total_count
integer i
integer li_Count_i
string ls_Type
string ls_Id
string ls_Sequence_id
string ls_Etag
string ls_Sha1
string ls_Name
string ls_Description
integer li_Size
integer li_Path_collectionTotal_count
string ls_Created_at
string ls_Modified_at
integer li_Trashed_at
integer li_Purged_at
string ls_Content_created_at
string ls_Content_modified_at
string ls_Created_byType
string ls_Created_byId
string ls_Created_byName
string ls_Created_byLogin
string ls_Modified_byType
string ls_Modified_byId
string ls_Modified_byName
string ls_Modified_byLogin
string ls_Owned_byType
string ls_Owned_byId
string ls_Owned_byName
string ls_Owned_byLogin
integer li_Shared_link
string ls_ParentType
string ls_ParentId
string ls_ParentSequence_id
string ls_ParentEtag
string ls_ParentName
string ls_Item_status
integer j
integer li_Count_j
integer li_Sequence_id_bool
integer li_Etag_bool

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat_9_5_0.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  URL: https://upload.box.com/api/2.0/files/content
li_BTls = 1
li_Port = 443
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("upload.box.com",li_Port,li_BTls,li_BAutoReconnect)
if li_Success <> 1 then
    Write-Debug "ConnectFailReason: " + string(loo_Rest.ConnectFailReason)
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    return
end if

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

loo_Rest.PartSelector = "2"
loo_FileStream2 = create oleobject
li_rc = loo_FileStream2.ConnectToNewObject("Chilkat_9_5_0.Stream")

loo_FileStream2.SourceFile = "tigers.jpeg"
loo_Rest.AddHeader("Content-Disposition","form-data; name=\"tigers.jpeg\"; filename=\"tigers.jpeg\"")
loo_Rest.AddHeader("Content-Type","image/jpeg")
loo_Rest.SetMultipartBodyStream(loo_FileStream2)

loo_Rest.PartSelector = "0"

loo_Rest.AddHeader("Authorization","Bearer BOX_ACCESS_TOKEN")

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

ls_StrResponseBody = loo_Rest.FullRequestMultipart("POST","/api/2.0/files/content")
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_FileStream2
    return
end if

loo_JsonResponse = create oleobject
li_rc = loo_JsonResponse.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_JsonResponse.Load(ls_StrResponseBody)

li_Total_count = loo_JsonResponse.IntOf("total_count")
i = 0
li_Count_i = loo_JsonResponse.SizeOfArray("entries")
do while i < li_Count_i
    loo_JsonResponse.I = i
    ls_Type = loo_JsonResponse.StringOf("entries[i].type")
    ls_Id = loo_JsonResponse.StringOf("entries[i].id")
    ls_Sequence_id = loo_JsonResponse.StringOf("entries[i].sequence_id")
    ls_Etag = loo_JsonResponse.StringOf("entries[i].etag")
    ls_Sha1 = loo_JsonResponse.StringOf("entries[i].sha1")
    ls_Name = loo_JsonResponse.StringOf("entries[i].name")
    ls_Description = loo_JsonResponse.StringOf("entries[i].description")
    li_Size = loo_JsonResponse.IntOf("entries[i].size")
    li_Path_collectionTotal_count = loo_JsonResponse.IntOf("entries[i].path_collection.total_count")
    ls_Created_at = loo_JsonResponse.StringOf("entries[i].created_at")
    ls_Modified_at = loo_JsonResponse.StringOf("entries[i].modified_at")
    li_Trashed_at = loo_JsonResponse.IsNullOf("entries[i].trashed_at")
    li_Purged_at = loo_JsonResponse.IsNullOf("entries[i].purged_at")
    ls_Content_created_at = loo_JsonResponse.StringOf("entries[i].content_created_at")
    ls_Content_modified_at = loo_JsonResponse.StringOf("entries[i].content_modified_at")
    ls_Created_byType = loo_JsonResponse.StringOf("entries[i].created_by.type")
    ls_Created_byId = loo_JsonResponse.StringOf("entries[i].created_by.id")
    ls_Created_byName = loo_JsonResponse.StringOf("entries[i].created_by.name")
    ls_Created_byLogin = loo_JsonResponse.StringOf("entries[i].created_by.login")
    ls_Modified_byType = loo_JsonResponse.StringOf("entries[i].modified_by.type")
    ls_Modified_byId = loo_JsonResponse.StringOf("entries[i].modified_by.id")
    ls_Modified_byName = loo_JsonResponse.StringOf("entries[i].modified_by.name")
    ls_Modified_byLogin = loo_JsonResponse.StringOf("entries[i].modified_by.login")
    ls_Owned_byType = loo_JsonResponse.StringOf("entries[i].owned_by.type")
    ls_Owned_byId = loo_JsonResponse.StringOf("entries[i].owned_by.id")
    ls_Owned_byName = loo_JsonResponse.StringOf("entries[i].owned_by.name")
    ls_Owned_byLogin = loo_JsonResponse.StringOf("entries[i].owned_by.login")
    li_Shared_link = loo_JsonResponse.IsNullOf("entries[i].shared_link")
    ls_ParentType = loo_JsonResponse.StringOf("entries[i].parent.type")
    ls_ParentId = loo_JsonResponse.StringOf("entries[i].parent.id")
    ls_ParentSequence_id = loo_JsonResponse.StringOf("entries[i].parent.sequence_id")
    ls_ParentEtag = loo_JsonResponse.StringOf("entries[i].parent.etag")
    ls_ParentName = loo_JsonResponse.StringOf("entries[i].parent.name")
    ls_Item_status = loo_JsonResponse.StringOf("entries[i].item_status")
    j = 0
    li_Count_j = loo_JsonResponse.SizeOfArray("entries[i].path_collection.entries")
    do while j < li_Count_j
        loo_JsonResponse.J = j
        ls_Type = loo_JsonResponse.StringOf("entries[i].path_collection.entries[j].type")
        ls_Id = loo_JsonResponse.StringOf("entries[i].path_collection.entries[j].id")
        li_Sequence_id_bool = loo_JsonResponse.IsNullOf("entries[i].path_collection.entries[j].sequence_id")
        li_Etag_bool = loo_JsonResponse.IsNullOf("entries[i].path_collection.entries[j].etag")
        ls_Name = loo_JsonResponse.StringOf("entries[i].path_collection.entries[j].name")
        j = j + 1
    loop
    i = i + 1
loop


destroy loo_Rest
destroy loo_FileStream2
destroy loo_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"
    }
  ]
}