SQL Server 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

SQL Server Example

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    --  URL: https://upload.box.com/api/2.0/files/content
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'upload.box.com', STR(@port), STR(@bTls), STR(@bAutoReconnect)
    IF STR(@success) <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    EXEC sp_OASetProperty @rest, 'PartSelector', '1'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Disposition', 'form-data; name="attributes"'
    EXEC sp_OAMethod @rest, 'SetMultipartBodyString', @success OUT, '{"name":"tigers.jpeg", "parent":{"id":"BOX_FOLDER_ID"}}'

    EXEC sp_OASetProperty @rest, 'PartSelector', '2'
    DECLARE @fileStream2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Stream', @fileStream2 OUT

    EXEC sp_OASetProperty @fileStream2, 'SourceFile', 'tigers.jpeg'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Disposition', 'form-data; name="tigers.jpeg"; filename="tigers.jpeg"'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'image/jpeg'
    EXEC sp_OAMethod @rest, 'SetMultipartBodyStream', @success OUT, STR(@fileStream2)

    EXEC sp_OASetProperty @rest, 'PartSelector', '0'

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Authorization', 'Bearer BOX_ACCESS_TOKEN'

    EXEC sp_OASetProperty @rest, 'PartSelector', '0'
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'multipart/form-data'

    DECLARE @strResponseBody nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestMultipart', @strResponseBody OUT, 'POST', '/api/2.0/files/content'
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @fileStream2
        RETURN
      END

    DECLARE @jsonResponse int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonResponse OUT

    EXEC sp_OAMethod @jsonResponse, 'Load', @success OUT, @strResponseBody

    DECLARE @total_count int

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @type nvarchar(4000)

    DECLARE @id nvarchar(4000)

    DECLARE @sequence_id nvarchar(4000)

    DECLARE @etag nvarchar(4000)

    DECLARE @sha1 nvarchar(4000)

    DECLARE @name nvarchar(4000)

    DECLARE @description nvarchar(4000)

    DECLARE @size int

    DECLARE @path_collectionTotal_count int

    DECLARE @created_at nvarchar(4000)

    DECLARE @modified_at nvarchar(4000)

    DECLARE @trashed_at int

    DECLARE @purged_at int

    DECLARE @content_created_at nvarchar(4000)

    DECLARE @content_modified_at nvarchar(4000)

    DECLARE @created_byType nvarchar(4000)

    DECLARE @created_byId nvarchar(4000)

    DECLARE @created_byName nvarchar(4000)

    DECLARE @created_byLogin nvarchar(4000)

    DECLARE @modified_byType nvarchar(4000)

    DECLARE @modified_byId nvarchar(4000)

    DECLARE @modified_byName nvarchar(4000)

    DECLARE @modified_byLogin nvarchar(4000)

    DECLARE @owned_byType nvarchar(4000)

    DECLARE @owned_byId nvarchar(4000)

    DECLARE @owned_byName nvarchar(4000)

    DECLARE @owned_byLogin nvarchar(4000)

    DECLARE @shared_link int

    DECLARE @parentType nvarchar(4000)

    DECLARE @parentId nvarchar(4000)

    DECLARE @parentSequence_id nvarchar(4000)

    DECLARE @parentEtag nvarchar(4000)

    DECLARE @parentName nvarchar(4000)

    DECLARE @item_status nvarchar(4000)

    DECLARE @j int

    DECLARE @count_j int

    DECLARE @sequence_id_bool int

    DECLARE @etag_bool int

    EXEC sp_OAMethod @jsonResponse, 'IntOf', @total_count OUT, 'total_count'
    SELECT @i = 0
    EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'entries'
    WHILE STR(@i) < STR(@count_i)
      BEGIN
        EXEC sp_OASetProperty @jsonResponse, 'I', STR(@i)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @type OUT, 'entries[i].type'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'entries[i].id'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @sequence_id OUT, 'entries[i].sequence_id'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @etag OUT, 'entries[i].etag'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @sha1 OUT, 'entries[i].sha1'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @name OUT, 'entries[i].name'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @description OUT, 'entries[i].description'
        EXEC sp_OAMethod @jsonResponse, 'IntOf', @size OUT, 'entries[i].size'
        EXEC sp_OAMethod @jsonResponse, 'IntOf', @path_collectionTotal_count OUT, 'entries[i].path_collection.total_count'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @created_at OUT, 'entries[i].created_at'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @modified_at OUT, 'entries[i].modified_at'
        EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @trashed_at OUT, 'entries[i].trashed_at'
        EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @purged_at OUT, 'entries[i].purged_at'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @content_created_at OUT, 'entries[i].content_created_at'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @content_modified_at OUT, 'entries[i].content_modified_at'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @created_byType OUT, 'entries[i].created_by.type'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @created_byId OUT, 'entries[i].created_by.id'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @created_byName OUT, 'entries[i].created_by.name'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @created_byLogin OUT, 'entries[i].created_by.login'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @modified_byType OUT, 'entries[i].modified_by.type'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @modified_byId OUT, 'entries[i].modified_by.id'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @modified_byName OUT, 'entries[i].modified_by.name'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @modified_byLogin OUT, 'entries[i].modified_by.login'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @owned_byType OUT, 'entries[i].owned_by.type'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @owned_byId OUT, 'entries[i].owned_by.id'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @owned_byName OUT, 'entries[i].owned_by.name'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @owned_byLogin OUT, 'entries[i].owned_by.login'
        EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @shared_link OUT, 'entries[i].shared_link'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @parentType OUT, 'entries[i].parent.type'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @parentId OUT, 'entries[i].parent.id'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @parentSequence_id OUT, 'entries[i].parent.sequence_id'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @parentEtag OUT, 'entries[i].parent.etag'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @parentName OUT, 'entries[i].parent.name'
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @item_status OUT, 'entries[i].item_status'
        SELECT @j = 0
        EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_j OUT, 'entries[i].path_collection.entries'
        WHILE STR(@j) < STR(@count_j)
          BEGIN
            EXEC sp_OASetProperty @jsonResponse, 'J', STR(@j)
            EXEC sp_OAMethod @jsonResponse, 'StringOf', @type OUT, 'entries[i].path_collection.entries[j].type'
            EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'entries[i].path_collection.entries[j].id'
            EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @sequence_id_bool OUT, 'entries[i].path_collection.entries[j].sequence_id'
            EXEC sp_OAMethod @jsonResponse, 'IsNullOf', @etag_bool OUT, 'entries[i].path_collection.entries[j].etag'
            EXEC sp_OAMethod @jsonResponse, 'StringOf', @name OUT, 'entries[i].path_collection.entries[j].name'
            SELECT @j = STR(@j) + 1
          END
        SELECT @i = STR(@i) + 1
      END

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @fileStream2
    EXEC @hr = sp_OADestroy @jsonResponse


END
GO

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