Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Swift 2 Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Swift 2) MIME Content-Transfer-Encoding Header Field

Explains the Content-Transfer-Encoding header field and how it affects how data is stored in the MIME.

Chilkat Downloads for the Swift Programming Language

MAC OS X (Cocoa) Objective-C/Swift Libs

iOS Objective-C/Swift Libs

func chilkatTest() {
    // The Content-Transfer-Encoding header field is typically set to one of these values:
    // 
    //   base64
    //   quoted-printable
    //   binary
    //   8bit
    //   7bit

    // If the encoding is base64 or quoted-printable, then the bytes of the content are encoded as such.
    // The values "8bit", "7bit", and "binary" all imply that NO encoding has been performed.

    // Here are some guidelines and hints:
    // 
    // 1) "8bit", "7bit", and "binary" encodings (i.e. NO encoding) results in the smallest MIME.

    // 2) A "binary" encoding implies non-text data.  Binary MIME CANNOT be retrieved as a string.  It can only be retrieved as a byte array.

    // 3) "8bit" and "7bit" encodings imply text.  
    //    7bit implies that the text is us-ascii (all byte values less than or equal to 0x7F).
    //    8bit implies that the text contains non-us-ascii chars.

    // 4) Base64 is the best choice for encoding non-text data, such as PDF's, images, etc.

    // 5) quoted-printable is the best choice for encoding text data where most chars are going to be us-ascii. This would typically 
    //    include all Western European languages.  For Asian, Arabic, Hebrew, etc. where most chars in the text are non-us-ascii,
    //    the most efficient encoding would be base64.  

    // Let's demonstrate with this small JPG image.
    var success: Bool
    let sbJpgBase64 = CkoStringBuilder()
    sbJpgBase64.Append("/9j/4AAQSkZJRgABAQEASABIAAD//gAmRmlsZSB3cml0dGVuIGJ5IEFkb2JlIFBob3Rvc2hvcD8g\r\n")
    sbJpgBase64.Append("NC4w/9sAQwAQCwwODAoQDg0OEhEQExgoGhgWFhgxIyUdKDozPTw5Mzg3QEhcTkBEV0U3OFBtUVdf\r\n")
    sbJpgBase64.Append("YmdoZz5NcXlwZHhcZWdj/9sAQwEREhIYFRgvGhovY0I4QmNjY2NjY2NjY2NjY2NjY2NjY2NjY2Nj\r\n")
    sbJpgBase64.Append("Y2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2Nj/8IAEQgAFAAUAwERAAIRAQMRAf/EABcAAAMBAAAA\r\n")
    sbJpgBase64.Append("AAAAAAAAAAAAAAIDBAX/xAAYAQADAQEAAAAAAAAAAAAAAAABAgMEAP/aAAwDAQACEAMQAAAB2kZY\r\n")
    sbJpgBase64.Append("NNEijWKddfTmLgALWH//xAAbEAACAgMBAAAAAAAAAAAAAAABAgMRAAQSE//aAAgBAQABBQL0XqN+\r\n")
    sbJpgBase64.Append("pM2aqJGMiqFFCyg7z//EABwRAAICAgMAAAAAAAAAAAAAAAERAAIQIQMSUf/aAAgBAwEBPwHqU5aq\r\n")
    sbJpgBase64.Append("Axx+y1tMQl4elj//xAAcEQEAAQUBAQAAAAAAAAAAAAABEQACEBIhA1H/2gAIAQIBAT8B3Bhqy7Zc\r\n")
    sbJpgBase64.Append("enyiwmGgDhiOzj//xAAdEAABAwUBAAAAAAAAAAAAAAABAAIREBIhIkFR/9oACAEBAAY/ArZyn+Cg\r\n")
    sbJpgBase64.Append("xtxWuJaoCnqDuin/xAAcEAABBAMBAAAAAAAAAAAAAAABABEhYRAxQVH/2gAIAQEAAT8hkEwPUUR9\r\n")
    sbJpgBase64.Append("DYfE4nxtRpIkBTsayuALIiuY/9oADAMBAAIAAwAAABDWPTsf/8QAGhEAAwADAQAAAAAAAAAAAAAA\r\n")
    sbJpgBase64.Append("AAEREDFBIf/aAAgBAwEBPxC0DVPcWm+Ce4OesrkE6bjH/8QAGBEBAQEBAQAAAAAAAAAAAAAAAREA\r\n")
    sbJpgBase64.Append("QRD/2gAIAQIBAT8QahMiOc8YgSrnTY3ELclHXn//xAAcEAEBAAIDAQEAAAAAAAAAAAABEQAhMUFx\r\n")
    sbJpgBase64.Append("EFH/2gAIAQEAAT8Qn3igmSZSj+c4N4zapMy9IjFV98wncN2iuLFsCEbDGxQkI6RO/n//2Q==\r\n")

    let mime = CkoMime()
    mime.ContentType = "image/jpeg"
    mime.SetBodyFromEncoded("base64", str: sbJpgBase64.GetAsString())

    print("\(mime.GetMime())")
    print("-")

    // The result:
    // 
    // 	Content-Type: image/jpeg
    // 	Content-Transfer-Encoding: base64
    // 
    // 	/9j/4AAQSkZJRgABAQEASABIAAD//gAm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2iuLFsCEbDGxQkI6RO/n//2Q==
    // 

    // Set the Content-Transfer-Encoding to "binary" by setting the Encoding property.
    mime.Encoding = "binary"

    // Try to get the MIME string.  This CANNOT be done.  The bytes of the JPG image do not
    // represent chars, and to return a string means that bytes must be interpreted according
    // to some character encoding (such as utf-8).  Non-text binary bytes can only be 
    // contained in a string IF encoded in some way. Encodings such as Base64, quoted-printable,
    // URL, etc. exist to make it possible to represent binary data in string format.   
    print("\(mime.GetMime())")
    print("-")

    // We CAN get the binary MIME as bytes..
    var mimeBytes: NSData
    mimeBytes = mime.GetMimeBytes()

    // Regardless of the Content-Transfer-Encoding, the 
    // body content can always be retrieved and the body bytes
    // decoded from whatever encoding is used..
    var jpgBytes: NSData
    jpgBytes = mime.GetBodyBinary()

    // To get the body in base64 format, first make sure
    // the Content-Transfer-Encoding is base64, then call GetBodyEncoded.
    mime.Encoding = "base64"
    var jpgBase64: String? = mime.GetBodyEncoded()
    print("\(jpgBase64!)")
    print("-")

    // Let's go back to "binary" MIME..
    mime.Encoding = "binary"

    // Let's say we have MIME, and it was loaded directly from a file, or from
    // a byte array.  (It was not loaded from the contents of a string variable.)
    // We don't know whether the MIME contains binary or 8bit encodings, and thus
    // we dont' know if the MIME is safe to get as a string.
    // The Convert8Bit method can be called to recursively traverse the MIME and set
    // all 8bit or binary encodings to "base64".  This makes the MIME safe for storing in
    // a string.
    mime.Convert8Bit()
    var mimeStr: String? = mime.GetMime()
    print("\(mimeStr!)")
    print("-")

}

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.