Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Box.com Streaming Upload File

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 ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

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.