Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vBd
    Handle hoBd
    Handle hoMime
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  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.
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get ComLoadFile Of hoBd "qa_data/photo.jpg" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoBd To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End

    //  Set a Content-Type for the binary content (SetBodyBd does not set one itself).
    Set ComContentType Of hoMime To "image/jpeg"

    Get pvComObject of hoBd to vBd
    Get ComSetBodyBd Of hoMime vBd To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumBytes Of hoBd To iTemp1
    Showln "Body set from " iTemp1 " bytes."


End_Procedure