Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) Box.com Streaming Upload FileDemonstrates 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.
Use ChilkatAx-win32.pkg Procedure Test Handle hoRest Boolean iSuccess Variant vOauth2 Handle hoOauth2 Boolean iBAutoReconnect Handle hoJsonAttr Variant vFileStream Handle hoFileStream String sResponseBody String sTemp1 Integer iTemp1 Boolean bTemp1 // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Get Create (RefClass(cComChilkatRest)) To hoRest If (Not(IsComObjectCreated(hoRest))) Begin Send CreateComObject of hoRest End // Provide a previously obtained OAuth2 access token. Get Create (RefClass(cComChilkatOAuth2)) To hoOauth2 If (Not(IsComObjectCreated(hoOauth2))) Begin Send CreateComObject of hoOauth2 End Set ComAccessToken Of hoOauth2 To "BOX_ACCESS_TOKEN" Get pvComObject of hoOauth2 to vOauth2 Get ComSetAuthOAuth2 Of hoRest vOauth2 To iSuccess // 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. Move True To iBAutoReconnect // ---------------------------------------------------------------------- // IMPORTANT: Note that the domain is "upload.box.com", not "api.box.com" // ---------------------------------------------------------------------- Get ComConnect Of hoRest "upload.box.com" 443 True iBAutoReconnect To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End // 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.) Get ComAddHeader Of hoRest "Content-Type" "multipart/form-data" To iSuccess // 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"}} Get Create (RefClass(cComChilkatJsonObject)) To hoJsonAttr If (Not(IsComObjectCreated(hoJsonAttr))) Begin Send CreateComObject of hoJsonAttr End Get ComUpdateString Of hoJsonAttr "name" "hedgehogs.jpg" To iSuccess Get ComUpdateString Of hoJsonAttr "parent.id" "0" To iSuccess Set ComPartSelector Of hoRest To "1" Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="attributes"; ' To iSuccess Get ComEmit Of hoJsonAttr To sTemp1 Get ComSetMultipartBodyString Of hoRest sTemp1 To iSuccess // The upload will stream directly from a file. Set ComPartSelector Of hoRest To "2" Get ComAddHeader Of hoRest "Content-Disposition" 'form-data; name="file"; filename="hedgehogs.jpg"' To iSuccess // "application/octet-stream" can be safely used for any type file.. Get ComAddHeader Of hoRest "Content-Type" "application/octet-stream" To iSuccess // IMPORTANT: This example requires Chilkat v9.5.0.70 or later, for a fix that was made in // multipart/streaming uploads. Get Create (RefClass(cComChilkatStream)) To hoFileStream If (Not(IsComObjectCreated(hoFileStream))) Begin Send CreateComObject of hoFileStream End Set ComSourceFile Of hoFileStream To "qa_data/jpg/hedgehogs.jpg" Get pvComObject of hoFileStream to vFileStream Get ComSetMultipartBodyStream Of hoRest vFileStream To iSuccess // Restore the PartSelector to "0" (for safety, in case something else sends another request on this object) Set ComPartSelector Of hoRest To "0" // Send the multipart/form-data request, which uploads the file by streaming directly from the filesystem. Get ComFullRequestMultipart Of hoRest "POST" "/api/2.0/files/content" To sResponseBody Get ComLastMethodSuccess Of hoRest To bTemp1 If (bTemp1 <> True) Begin Get ComLastErrorText Of hoRest To sTemp1 Showln sTemp1 Procedure_Return End // A 201 is received for a successful upload Get ComResponseStatusCode Of hoRest To iTemp1 If (iTemp1 <> 201) Begin Showln "Box.com upload failed." Showln "Request header:" Get ComLastRequestHeader Of hoRest To sTemp1 Showln sTemp1 Showln "---" Get ComResponseStatusCode Of hoRest To iTemp1 Showln "Response status code = " iTemp1 Showln "Response body:" Showln sResponseBody Procedure_Return End Showln "File uploaded." End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.