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
(SQL Server) Download File from Dropbox into a String VariableDemonstrates how to download a file from Dropbox directly into a string variable.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int DECLARE @iTmp1 int DECLARE @iTmp2 int DECLARE @iTmp3 int DECLARE @iTmp4 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). 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 -- Use "Chilkat_9_5_0.Rest" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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": "/jack.txt", -- } DECLARE @json int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT EXEC sp_OAMethod @json, 'AppendString', @success OUT, 'path', '/jack.txt' EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Dropbox-API-Arg', @sTmp0 -- The content of the file on Dropbox is returned. DECLARE @fileContent nvarchar(4000) EXEC sp_OAMethod @rest, 'FullRequestNoBody', @fileContent 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 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): ' + @fileContent 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 RETURN END -- Show the file content that was downloaded: PRINT @fileContent PRINT '----' -- 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": "jack.txt", -- "path_lower": "/jack.txt", -- "path_display": "/jack.txt", -- "id": "id:yqx4-tE_NKAAAAAAAAAAAQ", -- "client_modified": "2016-06-02T20:42:11Z", -- "server_modified": "2016-06-02T20:42:11Z", -- "rev": "8482db15f", -- "size": 42 -- } -- Load the JSON, pretty-print it, and demonstrate how to get some values... DECLARE @jsonResult int -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 -- Use "Chilkat_9_5_0.CkDateTime" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 @jsonResult EXEC @hr = sp_OADestroy @ckdt END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.