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
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) Upload File in Blocks (with Content-MD5 header) and Commit the Block List

Demonstrates how to upload a file in blocks and then commit the block list. This example includes a Content-MD5 header for each block.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoRest
    Boolean iBTls
    Integer iPort
    Boolean iBAutoReconnect
    Boolean iSuccess
    Variant vAzAuth
    Handle hoAzAuth
    Handle hoXml
    Handle hoFac
    Integer iBlockSize
    Integer iNumBlocks
    Handle hoCrypt
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Handle hoUriPath
    String sBlockId
    Variant vDataBlock
    Handle hoDataBlock
    String sContentMd5
    Integer i
    String sXmlStr
    String sResponseStr
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    // Azure Blob Service Example: Upload a file in blocks, and then commit the block list.
    // See also: https://msdn.microsoft.com/en-us/library/azure/dd135726.aspx

    // This example 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

    // Connect to the Azure Storage Blob Service
    Move True To iBTls
    Move 443 To iPort
    Move True To iBAutoReconnect
    // In this example, the storage account name is "chilkat".
    Get ComConnect Of hoRest "chilkat.blob.core.windows.net" iPort iBTls iBAutoReconnect To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Provide Azure Cloud credentials for the REST call.
    Get Create (RefClass(cComChilkatAuthAzureStorage)) To hoAzAuth
    If (Not(IsComObjectCreated(hoAzAuth))) Begin
        Send CreateComObject of hoAzAuth
    End
    Set ComAccessKey Of hoAzAuth To "AZURE_ACCESS_KEY"
    // The account name used here should match the 1st part of the domain passed in the call to Connect (above).
    Set ComAccount Of hoAzAuth To "chilkat"
    Set ComScheme Of hoAzAuth To "SharedKey"
    Set ComService Of hoAzAuth To "Blob"
    // This causes the "x-ms-version: 2021-08-06" header to be automatically added.
    Set ComXMsVersion Of hoAzAuth To "2021-08-06"
    Get pvComObject of hoAzAuth to vAzAuth
    Get ComSetAuthAzureStorage Of hoRest vAzAuth To iSuccess

    // Note: The application does not need to explicitly set the following
    // headers: Content-Length, x-ms-date, Authorization.  These headers
    // are automatically set by Chilkat.

    // As the blocks are uploaded, we'll keep an XML block list for the subsequent commit..
    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "BlockList"

    // Any type of file can be uploaded in this way.  It can a text file, binary file, anything...
    // This example will upload an XML file that is approximately 275K in size.  It can be downloaded
    // at http://www.chilkatsoft.com/hamlet.xml
    Get Create (RefClass(cComCkFileAccess)) To hoFac
    If (Not(IsComObjectCreated(hoFac))) Begin
        Send CreateComObject of hoFac
    End
    Get ComOpenForRead Of hoFac "qa_data/xml/hamlet.xml" To iSuccess
    // Assuming success for the example..

    // We'll upload in 16K blocks (normally a program would upload in larger block sizes than this,
    // but this is just an example...)
    Move 16384 To iBlockSize

    // How many 16K blocks?  (Including 1 for the last partial block)
    Get ComGetNumBlocks Of hoFac iBlockSize To iNumBlocks

    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Set ComHashAlgorithm Of hoCrypt To "md5"

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoUriPath
    If (Not(IsComObjectCreated(hoUriPath))) Begin
        Send CreateComObject of hoUriPath
    End

    Get Create (RefClass(cComChilkatBinData)) To hoDataBlock
    If (Not(IsComObjectCreated(hoDataBlock))) Begin
        Send CreateComObject of hoDataBlock
    End

    Move 0 To i
    While (i < iNumBlocks)

        Get ComClear Of hoDataBlock To iSuccess
        Get pvComObject of hoDataBlock to vDataBlock
        Get ComReadBlockBd Of hoFac i iBlockSize vDataBlock To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoFac To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        // Generate a base64 block ID.  
        // (Chilkat provides a helper method named GenBlockId to make this easy)
        // A pre-base64 encoded block ID length of 4 is sufficient in this case because
        // this file certainly won't have more than 99,999 blocks..
        Get ComGenBlockId Of hoFac i 4 "base64" To sBlockId

        // Add this blockId to the list of blocks to be committed.
        Send ComNewChild2 To hoXml "Latest" sBlockId

        // Build the URI path
        Send ComClear To hoUriPath
        Get ComAppend Of hoUriPath "/mycontainer/hamlet.xml?comp=block&blockId=" To iSuccess
        Get ComAppend Of hoUriPath sBlockId To iSuccess

        Get pvComObject of hoDataBlock to vDataBlock
        Get ComHashBdENC Of hoCrypt vDataBlock To sContentMd5
        Get ComAddHeader Of hoRest "Content-MD5" sContentMd5 To iSuccess

        // Upload this block..
        Send ComClear To hoSbResponseBody
        Get ComGetAsString Of hoUriPath To sTemp1
        Get pvComObject of hoDataBlock to vDataBlock
        Get pvComObject of hoSbResponseBody to vSbResponseBody
        Get ComFullRequestBd Of hoRest "PUT" sTemp1 vDataBlock vSbResponseBody To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoRest To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        // Verify that we received a 201 status code.
        Get ComResponseStatusCode Of hoRest To iTemp1
        If (iTemp1 <> 201) Begin
            // Examine the request/response to see what happened.
            Get ComResponseStatusCode Of hoRest To iTemp1
            Showln "response status code = " iTemp1
            Get ComResponseStatusText Of hoRest To sTemp1
            Showln "response status text = " sTemp1
            Get ComResponseHeader Of hoRest To sTemp1
            Showln "response header: " sTemp1
            Get ComGetAsString Of hoSbResponseBody To sTemp1
            Showln "response body (if any): " sTemp1
            Showln "---"
            Get ComLastRequestStartLine Of hoRest To sTemp1
            Showln "LastRequestStartLine: " sTemp1
            Get ComLastRequestHeader Of hoRest To sTemp1
            Showln "LastRequestHeader: " sTemp1
            Procedure_Return
        End

        Move (i + 1) To i
    Loop

    Send ComFileClose To hoFac

    // Now commit the blocks.
    // Let's have a look at the XML that will commit the blocks:
    Get ComGetXml Of hoXml To sXmlStr
    Showln sXmlStr

    // The XML will look like this:

    // <?xml version="1.0" encoding="utf-8" ?>
    // <BlockList>
    //     <Latest>MDAwMA==</Latest>
    //     <Latest>MDAwMQ==</Latest>
    //     <Latest>MDAwMg==</Latest>
    //     <Latest>MDAwMw==</Latest>
    //     <Latest>MDAwNA==</Latest>
    //     <Latest>MDAwNQ==</Latest>
    //     <Latest>MDAwNg==</Latest>
    //     <Latest>MDAwNw==</Latest>
    //     <Latest>MDAwOA==</Latest>
    //     <Latest>MDAwOQ==</Latest>
    //     <Latest>MDAxMA==</Latest>
    //     <Latest>MDAxMQ==</Latest>
    //     <Latest>MDAxMg==</Latest>
    //     <Latest>MDAxMw==</Latest>
    //     <Latest>MDAxNA==</Latest>
    //     <Latest>MDAxNQ==</Latest>
    //     <Latest>MDAxNg==</Latest>
    //     <Latest>MDAxNw==</Latest>
    // </BlockList>

    // --------------------------------------------------------------------------
    // IMPORTANT: Remove the Content-MD5 header previously set in the loop above.
    // --------------------------------------------------------------------------
    Get ComRemoveHeader Of hoRest "Content-MD5" To iSuccess

    // Send the PUT Block List...
    Get ComFullRequestString Of hoRest "PUT" "/mycontainer/hamlet.xml?comp=blocklist" sXmlStr To sResponseStr
    Get ComLastMethodSuccess Of hoRest To bTemp1
    If (bTemp1 <> True) Begin
        Get ComLastErrorText Of hoRest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // When successful, the Azure Storage service will respond with a 201 response status code,
    // with no response body.

    Get ComResponseStatusCode Of hoRest To iTemp1
    If (iTemp1 <> 201) Begin
        // Examine the request/response to see what happened.
        Get ComResponseStatusCode Of hoRest To iTemp1
        Showln "response status code = " iTemp1
        Get ComResponseStatusText Of hoRest To sTemp1
        Showln "response status text = " sTemp1
        Get ComResponseHeader Of hoRest To sTemp1
        Showln "response header: " sTemp1
        Showln "response body (if any): " sResponseStr
        Showln "---"
        Get ComLastRequestStartLine Of hoRest To sTemp1
        Showln "LastRequestStartLine: " sTemp1
        Get ComLastRequestHeader Of hoRest To sTemp1
        Showln "LastRequestHeader: " sTemp1
        Procedure_Return
    End

    Showln "Success."


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.