Xbase++
Xbase++
Box.com Streaming Upload File
See more Box Examples
Demonstrates how to upload a file to box.com, streaming the file directly from the filesystem.Note: This example requires a fix that is included in Chilkat v9.5.0.70 and above.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRest
LOCAL oOauth2
LOCAL nBAutoReconnect
LOCAL oJsonAttr
LOCAL oFileStream
LOCAL cResponseBody
nSuccess := 0
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oRest := CreateObject("Chilkat.Rest")
// Provide a previously obtained OAuth2 access token.
oOauth2 := CreateObject("Chilkat.OAuth2")
oOauth2:AccessToken := "BOX_ACCESS_TOKEN"
oRest:SetAuthOAuth2(oOauth2)
// First, make the initial connection.
// A single REST object, once connected, can be used for many Box REST API calls.
// The auto-reconnect indicates that if the already-established HTTPS connection is closed,
// then it will be automatically re-established as needed.
nBAutoReconnect := 1
// ----------------------------------------------------------------------
// IMPORTANT: Note that the domain is "upload.box.com", not "api.box.com"
// ----------------------------------------------------------------------
nSuccess := oRest:Connect("upload.box.com", 443, 1, nBAutoReconnect)
IF (nSuccess != 1)
? oRest:LastErrorText
oRest:destroy()
oOauth2:destroy()
RETURN
ENDIF
// The request body uses the "multipart/form-data" format to transmit two "parts".
// The first part is called "attributes" and contains a JSON object with information about the file, including the name of the file
// and the ID of the parent folder. The second part contains the contents of the file.
// (Note that the name of the second "part" is ignored.)
oRest:AddHeader("Content-Type", "multipart/form-data")
// Provide the content for each part of the request...
// First the JSON attributes. Use "0" for the root folder.
// {"name":"hedgehogs.jpg", "parent":{"id":"0"}}
oJsonAttr := CreateObject("Chilkat.JsonObject")
oJsonAttr:UpdateString("name", "hedgehogs.jpg")
oJsonAttr:UpdateString("parent.id", "0")
oRest:PartSelector := "1"
oRest:AddHeader("Content-Disposition", 'form-data; name="attributes"; ')
oRest:SetMultipartBodyString(oJsonAttr:Emit())
// The upload will stream directly from a file.
oRest:PartSelector := "2"
oRest:AddHeader("Content-Disposition", 'form-data; name="file"; filename="hedgehogs.jpg"')
// "application/octet-stream" can be safely used for any type file..
oRest:AddHeader("Content-Type", "application/octet-stream")
// IMPORTANT: This example requires Chilkat v9.5.0.70 or later, for a fix that was made in
// multipart/streaming uploads.
oFileStream := CreateObject("Chilkat.Stream")
oFileStream:SourceFile := "qa_data/jpg/hedgehogs.jpg"
oRest:SetMultipartBodyStream(oFileStream)
// Restore the PartSelector to "0" (for safety, in case something else sends another request on this object)
oRest:PartSelector := "0"
// Send the multipart/form-data request, which uploads the file by streaming directly from the filesystem.
cResponseBody := oRest:FullRequestMultipart("POST", "/api/2.0/files/content")
IF (oRest:LastMethodSuccess != 1)
? oRest:LastErrorText
oRest:destroy()
oOauth2:destroy()
oJsonAttr:destroy()
oFileStream:destroy()
RETURN
ENDIF
// A 201 is received for a successful upload
IF (oRest:ResponseStatusCode != 201)
? "Box.com upload failed."
? "Request header:"
? oRest:LastRequestHeader
? "---"
? "Response status code = " + Str(oRest:ResponseStatusCode)
? "Response body:"
? cResponseBody
oRest:destroy()
oOauth2:destroy()
oJsonAttr:destroy()
oFileStream:destroy()
RETURN
ENDIF
? "File uploaded."
oRest:destroy()
oOauth2:destroy()
oJsonAttr:destroy()
oFileStream:destroy()