Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.6.0+

OneDrive: Upload Binary Data from Memory

See more OneDrive Examples

Simple PUT upload now supports files up to 250 MB; the old 4 MB statement was obsolete. This example sets conflictBehavior=fail so an existing name is not silently replaced. Choose replace or rename deliberately if needed. A new file normally returns 201; replacement can return 200. The body is the raw file data, not multipart/form-data. Save the returned ID.

BinData contains raw bytes, including zero and non-ASCII bytes. HttpBd sends these bytes unchanged. This example creates a small local fixture instead of downloading unrelated data with a Graph-authenticated Http object. For large local files prefer streaming or an upload session.

Keep drive IDs and item IDs as opaque strings, retaining the drive context with each item. Encode individual path segments, not an entire URL or a slash-separated path in one call. An item ID remains stable across rename/move within its drive; a name-based path changes. OneDrive does not use Outlook's ImmutableId preference.

Live operation: this changes OneDrive data. Select the target deliberately. The example does not automatically retry uncertain writes.

Permission: Files.ReadWrite.

Uses direct Chilkat.Http requests to Microsoft Graph v1.0 in the global cloud. The delegated token is read from the private qa_data/tokens/oneDrive.json file created by the PKCE authorization example. Personal Microsoft accounts and Microsoft 365 work/school accounts can use delegated authorization when the app registration supports them. Refresh expired tokens; scopes and actual file access both matter.

For app-only Microsoft 365 access, use client credentials with administrator-approved application permissions. /me requires a signed-in user; app-only calls must address an authorized user, site, or drive explicitly. Client credentials do not grant access to personal OneDrive accounts. National clouds use their own authority and Graph hosts.

Check the expected HTTP status before using a response body. A false method result can still contain an HTTP error response; status zero means no response arrived. For 401, renew authorization; for 403, check permissions, access grants, and policy; for 429, honor Retry-After with a bounded retry. Reconcile uncertain writes before repeating them. Keep credentials, file contents, upload/download URLs, and detailed diagnostics private.

Microsoft documentation

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
#  Obtain a delegated Graph token with this example's file permissions.
#  See OneDrive OAuth2 with PKCE.
set token [new_CkJsonObject]

CkJsonObject_put_Strict $token 1
set success [CkJsonObject_LoadFile $token "qa_data/tokens/oneDrive.json"]
if {$success != 1} then {
    puts "Could not load the private OneDrive token cache."
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "access_token" "" 1] == 1]} then {
    puts "Expected a nonempty access_token in the JSON."
    delete_CkJsonObject $token
    exit
}

set http [new_CkHttp]

CkHttp_put_AuthToken $http [CkJsonObject_stringOf $token "access_token"]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
CkHttp_put_ConnectTimeout $http 20
CkHttp_put_ReadTimeout $http 120
CkHttp_SetRequestHeader $http "Accept" "application/json"
set resp [new_CkHttpResponse]

set result [new_CkJsonObject]

CkJsonObject_put_Strict $result 1
set driveId "YOUR_DRIVE_ID"
set parentId "YOUR_PARENT_FOLDER_ID"
set fileName "sample.bin"
set endpoint [new_CkStringBuilder]

CkStringBuilder_Append $endpoint "https://graph.microsoft.com/v1.0/drives/"
CkStringBuilder_Append $endpoint [CkHttp_urlEncode $http $driveId]
CkStringBuilder_Append $endpoint "/items/"
CkStringBuilder_Append $endpoint [CkHttp_urlEncode $http $parentId]
CkStringBuilder_Append $endpoint ":/"
CkStringBuilder_Append $endpoint [CkHttp_urlEncode $http $fileName]
CkStringBuilder_Append $endpoint ":/content?@microsoft.graph.conflictBehavior=fail"
#  The application can fill BinData from generated bytes. This fixture uses hexadecimal.
set data [new_CkBinData]

set success [CkBinData_AppendEncoded $data "0001027f80ff" "hex"]
if {$success != 1} then {
    puts "Could not prepare the binary data."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    delete_CkBinData $data
    exit
}

set success [CkHttp_HttpBd $http "PUT" [CkStringBuilder_getAsString $endpoint] $data "application/octet-stream" $resp]
if {expr [$success != 1]  &&  [[CkHttpResponse_get_StatusCode $resp] == 0]} then {
    puts "No HTTP response received; inspect diagnostics privately and reconcile writes before retrying."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    delete_CkBinData $data
    exit
}

if {expr [[CkHttpResponse_get_StatusCode $resp] != 200]  &&  [[CkHttpResponse_get_StatusCode $resp] != 201]} then {
    puts "Unexpected HTTP status: [CkHttpResponse_get_StatusCode $resp]"
    puts "Request ID: [CkHttpResponse_getHeaderField $resp request-id]"
    if {[CkHttpResponse_get_StatusCode $resp] == 429} then {
        puts "Retry-After: [CkHttpResponse_getHeaderField $resp Retry-After]"
    }

    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    delete_CkBinData $data
    exit
}

set success [CkJsonObject_Load $result [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
    puts "Expected a well-formed JSON response."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    delete_CkBinData $data
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $result "id"] != 1]  ||  [[CkJsonObject_StringOfEquals $result "id" "" 1] == 1]} then {
    puts "Expected a nonempty id in the JSON."
    delete_CkJsonObject $token
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $result
    delete_CkStringBuilder $endpoint
    delete_CkBinData $data
    exit
}

puts "Uploaded item ID: [CkJsonObject_stringOf $result id]"
puts "Stored name: [CkJsonObject_stringOf $result name]"
puts "Stored bytes: [CkJsonObject_stringOf $result size]"

delete_CkJsonObject $token
delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $result
delete_CkStringBuilder $endpoint
delete_CkBinData $data