VBScript
VBScript
Upload String to Dropbox
See more Dropbox Examples
Uploads a string to a file on Dropbox. This example can upload content up to 150MB, assuming a 150MB string fits in memory for your app. Larger files must be uploaded with an upload session (upload_session/start).Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' 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.
set rest = CreateObject("Chilkat.Rest")
' Connect to Dropbox
success = rest.Connect("content.dropboxapi.com",443,1,1)
If (success = 0) Then
outFile.WriteLine(rest.LastErrorText)
WScript.Quit
End If
' Add request headers.
success = rest.AddHeader("Content-Type","application/octet-stream")
success = rest.AddHeader("Authorization","Bearer DROPBOX_ACCESS_TOKEN")
' The upload "parameters" contained in JSON passed in an HTTP request header.
' This is the JSON to be added in this example:
' {
' "path": "/jack.txt",
' "mode": "add",
' "autorename": true,
' "mute": false
' }
' This will be the content of the file created in Dropbox.
content = "All work and no play makes Jack a dull boy"
set json = CreateObject("Chilkat.JsonObject")
success = json.AppendString("path","/jack.txt")
success = json.AppendString("mode","add")
success = json.AppendBool("autorename",1)
success = json.AppendBool("mute",0)
success = rest.AddHeader("Dropbox-API-Arg",json.Emit())
' Do the upload. The URL is https://content.dropboxapi.com/2/files/upload.
' We already connected to content.dropboxapi.com using TLS (i.e. HTTPS),
' so now we only need to specify the path "/2/files/upload".
responseStr = rest.FullRequestString("POST","/2/files/upload",content)
If (rest.LastMethodSuccess = 0) Then
outFile.WriteLine(rest.LastErrorText)
WScript.Quit
End If
' When successful, Dropbox responds with a 200 response code.
If (rest.ResponseStatusCode <> 200) Then
' Examine the request/response to see what happened.
outFile.WriteLine("response status code = " & rest.ResponseStatusCode)
outFile.WriteLine("response status text = " & rest.ResponseStatusText)
outFile.WriteLine("response header: " & rest.ResponseHeader)
outFile.WriteLine("response body (if any): " & responseStr)
outFile.WriteLine("---")
outFile.WriteLine("LastRequestStartLine: " & rest.LastRequestStartLine)
outFile.WriteLine("LastRequestHeader: " & rest.LastRequestHeader)
WScript.Quit
End If
outFile.WriteLine("LastRequestHeader: " & rest.LastRequestHeader)
' The response is JSON.
set jsonResp = CreateObject("Chilkat.JsonObject")
jsonResp.EmitCompact = 0
success = jsonResp.Load(responseStr)
' Show the JSON response.
outFile.WriteLine(jsonResp.Emit())
' Returns JSON that 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
' }
' Sample code to get data from the JSON response:
size = jsonResp.IntOf("size")
outFile.WriteLine("size = " & size)
rev = jsonResp.StringOf("rev")
outFile.WriteLine("rev = " & rev)
clientModified = jsonResp.StringOf("client_modified")
set ckdt = CreateObject("Chilkat.CkDateTime")
success = ckdt.SetFromTimestamp(clientModified)
bLocalTime = 1
set dt = CreateObject("Chilkat.DtObj")
ckdt.ToDtObj bLocalTime,dt
outFile.WriteLine(dt.Day & "/" & dt.Month & "/" & dt.Year & " " & dt.Hour & ":" & dt.Minute)
outFile.Close