Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

SQL Server Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(SQL Server) Download File from Dropbox

Downloads a file from Dropbox, streaming it directly to a file in the filesystem.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @iTmp2 int
    DECLARE @iTmp3 int
    DECLARE @iTmp4 int
    DECLARE @sTmp0 nvarchar(4000)
    --  This example requires the Chilkat API to have been previously unlocked.
    --  See Global Unlock Sample for sample code.

    --  A Dropbox access token should have been previously obtained.
    --  Dropbox access tokens do not expire.
    --  See Dropbox Access Token.

    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

    --  Connect to Dropbox
    DECLARE @success int
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'content.dropboxapi.com', 443, 1, 1
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    --  Add request headers.
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Authorization', 'Bearer DROPBOX_ACCESS_TOKEN'

    --  The download "parameters" are contained in JSON passed in an HTTP request header.
    --  This is the JSON indicating the file to be downloaded:
    --  {
    --     "path": "/Homework/lit/hamlet.xml",
    --  }

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

    EXEC sp_OAMethod @json, 'AppendString', @success OUT, 'path', '/Homework/lit/hamlet.xml'
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Dropbox-API-Arg', @sTmp0

    --  Setup a file stream for the download
    DECLARE @fileStream int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Stream', @fileStream OUT

    EXEC sp_OASetProperty @fileStream, 'SinkFile', 'qa_output/hamletFromDropbox.xml'

    --  Indicate that the call to FullRequestNoBody should send the response body
    --  to fileStream if the response status code is 200.
    --  If a non-success response status code is received, then nothing
    --  is streamed to the output file and the error response is returned by FullRequestNoBody.
    DECLARE @expectedStatus int
    SELECT @expectedStatus = 200
    EXEC sp_OAMethod @rest, 'SetResponseBodyStream', @success OUT, @expectedStatus, 1, @fileStream

    DECLARE @responseStr nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseStr OUT, 'POST', '/2/files/download'
    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 @json
        EXEC @hr = sp_OADestroy @fileStream
        RETURN
      END

    --  When successful, Dropbox responds with a 200 response code.
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN
        --  Examine the request/response to see what happened.

        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        PRINT 'response status code = ' + @iTmp0

        EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
        PRINT 'response status text = ' + @sTmp0

        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT 'response header: ' + @sTmp0

        PRINT 'response body (if any): ' + @responseStr

        PRINT '---'

        EXEC sp_OAGetProperty @rest, 'LastRequestStartLine', @sTmp0 OUT
        PRINT 'LastRequestStartLine: ' + @sTmp0

        EXEC sp_OAGetProperty @rest, 'LastRequestHeader', @sTmp0 OUT
        PRINT 'LastRequestHeader: ' + @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @fileStream
        RETURN
      END

    --  Information about the downloaded file is also available as JSON in a response header.
    --  The "dropbox-api-result" response header contains the information.  For example:
    DECLARE @apiResult nvarchar(4000)
    EXEC sp_OAMethod @rest, 'ResponseHdrByName', @apiResult OUT, 'dropbox-api-result'

    PRINT @apiResult

    --  In this case, the pretty-formatted dropbox-api-result JSON looks like this:
    --  {
    --    "name": "hamlet.xml",
    --    "path_lower": "/homework/lit/hamlet.xml",
    --    "path_display": "/Homework/lit/hamlet.xml",
    --    "id": "id:74FkdeNuyKAAAAAAAAAAAQ",
    --    "client_modified": "2016-06-02T23:19:00Z",
    --    "server_modified": "2016-06-02T23:19:00Z",
    --    "rev": "9482db15f",
    --    "size": 279658
    --  }

    --  Load the JSON, pretty-print it, and demonstrate how to get some values...
    DECLARE @jsonResult int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonResult OUT

    EXEC sp_OASetProperty @jsonResult, 'EmitCompact', 0
    EXEC sp_OAMethod @jsonResult, 'Load', @success OUT, @apiResult
    --  Show the JSON pretty-printed...
    EXEC sp_OAMethod @jsonResult, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    --  Sample code to get data from the JSON response:
    DECLARE @size int
    EXEC sp_OAMethod @jsonResult, 'IntOf', @size OUT, 'size'

    PRINT 'size = ' + @size

    DECLARE @rev nvarchar(4000)
    EXEC sp_OAMethod @jsonResult, 'StringOf', @rev OUT, 'rev'

    PRINT 'rev = ' + @rev

    DECLARE @clientModified nvarchar(4000)
    EXEC sp_OAMethod @jsonResult, 'StringOf', @clientModified OUT, 'client_modified'
    DECLARE @ckdt int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.CkDateTime', @ckdt OUT

    EXEC sp_OAMethod @ckdt, 'SetFromTimestamp', @success OUT, @clientModified
    DECLARE @bLocalTime int
    SELECT @bLocalTime = 1
    DECLARE @dt int
    EXEC sp_OAMethod @ckdt, 'GetDtObj', @dt OUT, @bLocalTime
    EXEC sp_OAGetProperty @dt, 'Day', @iTmp0 OUT

    EXEC sp_OAGetProperty @dt, 'Month', @iTmp1 OUT

    EXEC sp_OAGetProperty @dt, 'Year', @iTmp2 OUT

    EXEC sp_OAGetProperty @dt, 'Hour', @iTmp3 OUT

    EXEC sp_OAGetProperty @dt, 'Minute', @iTmp4 OUT
    PRINT @iTmp0 + '/' + @iTmp1 + '/' + @iTmp2 + ' ' + @iTmp3 + ':' + @iTmp4
    EXEC @hr = sp_OADestroy @dt


    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @fileStream
    EXEC @hr = sp_OADestroy @jsonResult
    EXEC @hr = sp_OADestroy @ckdt


END
GO

 

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