DataFlex 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

DataFlex Example

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoRest
    Boolean iSuccess
    Boolean iBTls
    Integer iPort
    Boolean iBAutoReconnect
    Variant vFileStream2
    Handle hoFileStream2
    String sStrResponseBody
    Handle hoJsonResponse
    Integer iTotal_count
    Integer i
    Integer iCount_i
    String sType
    String sId
    String sSequence_id
    String sEtag
    String sSha1
    String sName
    String sDescription
    Integer iSize
    Integer iPath_collectionTotal_count
    String sCreated_at
    String sModified_at
    Boolean iTrashed_at
    Boolean iPurged_at
    String sContent_created_at
    String sContent_modified_at
    String sCreated_byType
    String sCreated_byId
    String sCreated_byName
    String sCreated_byLogin
    String sModified_byType
    String sModified_byId
    String sModified_byName
    String sModified_byLogin
    String sOwned_byType
    String sOwned_byId
    String sOwned_byName
    String sOwned_byLogin
    Boolean iShared_link
    String sParentType
    String sParentId
    String sParentSequence_id
    String sParentEtag
    String sParentName
    String sItem_status
    Integer j
    Integer iCount_j
    Boolean iSequence_id_bool
    Boolean iEtag_bool
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Get Create (RefClass(cComChilkatRest)) To hoRest
    If (Not(IsComObjectCreated(hoRest))) Begin
        Send CreateComObject of hoRest
    End

    //  URL: https://upload.box.com/api/2.0/files/content
    Move True To iBTls
    Move 443 To iPort
    Move True To iBAutoReconnect
    Get ComConnect Of hoRest "upload.box.com" iPort iBTls iBAutoReconnect To iSuccess
    If (iSuccess <> True) Begin
        Get ComConnectFailReason Of hoRest To iTemp1
        Showln "ConnectFailReason: " iTemp1
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Set ComPartSelector Of hoRest To "1"
    Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="attributes"' To iSuccess
    Get ComSetMultipartBodyString Of hoRest '{"name":"tigers.jpeg", "parent":{"id":"BOX_FOLDER_ID"}}' To iSuccess

    Set ComPartSelector Of hoRest To "2"
    Get Create (RefClass(cComChilkatStream)) To hoFileStream2
    If (Not(IsComObjectCreated(hoFileStream2))) Begin
        Send CreateComObject of hoFileStream2
    End
    Set ComSourceFile Of hoFileStream2 To "tigers.jpeg"
    Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="tigers.jpeg"; filename="tigers.jpeg"' To iSuccess
    Get ComAddHeader Of hoRest "Content-Type" "image/jpeg" To iSuccess
    Get pvComObject of hoFileStream2 to vFileStream2
    Get ComSetMultipartBodyStream Of hoRest vFileStream2 To iSuccess

    Set ComPartSelector Of hoRest To "0"

    Get ComAddHeader Of hoRest "Authorization" "Bearer BOX_ACCESS_TOKEN" To iSuccess

    Set ComPartSelector Of hoRest To "0"
    Get ComAddHeader Of hoRest "Content-Type" "multipart/form-data" To iSuccess

    Get ComFullRequestMultipart Of hoRest "POST" "/api/2.0/files/content" To sStrResponseBody
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonResponse
    If (Not(IsComObjectCreated(hoJsonResponse))) Begin
        Send CreateComObject of hoJsonResponse
    End
    Get ComLoad Of hoJsonResponse sStrResponseBody To iSuccess

    Get ComIntOf Of hoJsonResponse "total_count" To iTotal_count
    Move 0 To i
    Get ComSizeOfArray Of hoJsonResponse "entries" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoJsonResponse To i
        Get ComStringOf Of hoJsonResponse "entries[i].type" To sType
        Get ComStringOf Of hoJsonResponse "entries[i].id" To sId
        Get ComStringOf Of hoJsonResponse "entries[i].sequence_id" To sSequence_id
        Get ComStringOf Of hoJsonResponse "entries[i].etag" To sEtag
        Get ComStringOf Of hoJsonResponse "entries[i].sha1" To sSha1
        Get ComStringOf Of hoJsonResponse "entries[i].name" To sName
        Get ComStringOf Of hoJsonResponse "entries[i].description" To sDescription
        Get ComIntOf Of hoJsonResponse "entries[i].size" To iSize
        Get ComIntOf Of hoJsonResponse "entries[i].path_collection.total_count" To iPath_collectionTotal_count
        Get ComStringOf Of hoJsonResponse "entries[i].created_at" To sCreated_at
        Get ComStringOf Of hoJsonResponse "entries[i].modified_at" To sModified_at
        Get ComIsNullOf Of hoJsonResponse "entries[i].trashed_at" To iTrashed_at
        Get ComIsNullOf Of hoJsonResponse "entries[i].purged_at" To iPurged_at
        Get ComStringOf Of hoJsonResponse "entries[i].content_created_at" To sContent_created_at
        Get ComStringOf Of hoJsonResponse "entries[i].content_modified_at" To sContent_modified_at
        Get ComStringOf Of hoJsonResponse "entries[i].created_by.type" To sCreated_byType
        Get ComStringOf Of hoJsonResponse "entries[i].created_by.id" To sCreated_byId
        Get ComStringOf Of hoJsonResponse "entries[i].created_by.name" To sCreated_byName
        Get ComStringOf Of hoJsonResponse "entries[i].created_by.login" To sCreated_byLogin
        Get ComStringOf Of hoJsonResponse "entries[i].modified_by.type" To sModified_byType
        Get ComStringOf Of hoJsonResponse "entries[i].modified_by.id" To sModified_byId
        Get ComStringOf Of hoJsonResponse "entries[i].modified_by.name" To sModified_byName
        Get ComStringOf Of hoJsonResponse "entries[i].modified_by.login" To sModified_byLogin
        Get ComStringOf Of hoJsonResponse "entries[i].owned_by.type" To sOwned_byType
        Get ComStringOf Of hoJsonResponse "entries[i].owned_by.id" To sOwned_byId
        Get ComStringOf Of hoJsonResponse "entries[i].owned_by.name" To sOwned_byName
        Get ComStringOf Of hoJsonResponse "entries[i].owned_by.login" To sOwned_byLogin
        Get ComIsNullOf Of hoJsonResponse "entries[i].shared_link" To iShared_link
        Get ComStringOf Of hoJsonResponse "entries[i].parent.type" To sParentType
        Get ComStringOf Of hoJsonResponse "entries[i].parent.id" To sParentId
        Get ComStringOf Of hoJsonResponse "entries[i].parent.sequence_id" To sParentSequence_id
        Get ComStringOf Of hoJsonResponse "entries[i].parent.etag" To sParentEtag
        Get ComStringOf Of hoJsonResponse "entries[i].parent.name" To sParentName
        Get ComStringOf Of hoJsonResponse "entries[i].item_status" To sItem_status
        Move 0 To j
        Get ComSizeOfArray Of hoJsonResponse "entries[i].path_collection.entries" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoJsonResponse To j
            Get ComStringOf Of hoJsonResponse "entries[i].path_collection.entries[j].type" To sType
            Get ComStringOf Of hoJsonResponse "entries[i].path_collection.entries[j].id" To sId
            Get ComIsNullOf Of hoJsonResponse "entries[i].path_collection.entries[j].sequence_id" To iSequence_id_bool
            Get ComIsNullOf Of hoJsonResponse "entries[i].path_collection.entries[j].etag" To iEtag_bool
            Get ComStringOf Of hoJsonResponse "entries[i].path_collection.entries[j].name" To sName
            Move j + 1 To j
        Loop

        Move i + 1 To i
    Loop



End_Procedure

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