Sample code for 30+ languages & platforms
Tcl

Set a MIME Body from Binary Bytes (BinData)

See more MIME Examples

Demonstrates the Chilkat Mime.SetBodyBd method, which sets the decoded body bytes from a BinData, changes the transfer encoding to base64, and preserves the bytes exactly. The only argument is the BinData.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the direct way to put arbitrary binary content into a part from memory — a generated image, a decrypted blob — without a temporary file. Chilkat base64-encodes it so the bytes survive text-based transports unchanged. Note it does not set a Content-Type or disposition, so assign those yourself to describe what the bytes are.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Mime.SetBodyBd method, which sets the decoded body bytes from a BinData,
#  changes the transfer encoding to base64, and preserves the bytes exactly.  The only argument is
#  the BinData.

#  Put the binary body bytes into a BinData.
set bd [new_CkBinData]

set success [CkBinData_LoadFile $bd "qa_data/photo.jpg"]
if {$success == 0} then {
    puts [CkBinData_lastErrorText $bd]
    delete_CkBinData $bd
    exit
}

set mime [new_CkMime]

#  Set a Content-Type for the binary content (SetBodyBd does not set one itself).
CkMime_put_ContentType $mime "image/jpeg"

set success [CkMime_SetBodyBd $mime $bd]
if {$success == 0} then {
    puts [CkMime_lastErrorText $mime]
    delete_CkBinData $bd
    delete_CkMime $mime
    exit
}

puts "Body set from [CkBinData_get_NumBytes $bd] bytes."

delete_CkBinData $bd
delete_CkMime $mime