Attachment Object
The Attachment object enables users to manage and access information about files downloaded to disk using the rest.downloadFile function. The downloaded attachments can be easily incorporated into message content.
For more details on downloading files using the rest.downloadFile function, refer to http.downloadFile LINK.
Attachment Methods
Here are the key methods available for the Attachment object:
-
attachment.getName()- Retrieves the full path of the file downloaded to disk.
-
attachment.getId()- Retrieves the attachment ID, if available.
-
attachment.getFilename()- Retrieves the full path of the file downloaded to disk.
-
attachment.getFile()- Returns the attachment as a
java.io.Fileobject.
- Returns the attachment as a
-
attachment.getBase64()- Returns the file content in Base64-encoded format.
-
attachment.getBytes()- Retrieves the file content as a byte array.
-
attachment.getMode()- Returns the mode of the attachment, which can be
base64,file,link, orinternal. In this context, it isfile.
- Returns the mode of the attachment, which can be
-
attachment.getContentType()- Retrieves the content type (MIME type) of the downloaded file.
Example Usage
In this example, we download an image file from a URL, retrieve its file path, convert it to Base64, and get the content type. Finally, the attachment is added to a message.
url = "https://www.w3.org/2008/site/images/logo-w3c-mobile-lg"
http = option.rest
http.rest.addParam("endpoint", "HTTP")
// Download the file and save it locally
attachment = http.downloadFile(url, "1", "logo-w3c-mobile-lg.png")
// Retrieve the file path
file = attachment.getFilename()
// Get the Base64-encoded content of the file
base64string = attachment.getBase64()
// Retrieve the content type of the file
contentType = attachment.getContentType()
// Create a new message with the type "HelloMessageType"
msg = api.newMessage("HelloMessageType")
msg.put("text", "Hello world")
// Add the attachment to the message
msg.put("attachments/0", attachment)