Add attachment from binary object

   —   
The most common way of adding attachments to documents through the API, is by using a Path or HttpPostedFile as a parameter for the AddAttachment() method. This article describes how to pass a binary object (e.g. image, video) retrieved directly from the database.
To create an attachment from a binary object, you have to create a new object of AttachmentInfo class and initialize it with proper parameters. The binary object is assigned to AttachmentBinary property of the newly created object.

The example could look like this:
AttachmentInfo newAttachment = new AttachmentInfo { AttachmentBinary = imageBinary, AttachmentDocumentID = 25, AttachmentExtension = "jpg", AttachmentSize = imageBinary.Length, AttachmentName = "newAttachment", AttachmentMimeType = MimeTypeHelper.GetMimetype("jpg"), AttachmentTitle = "attachmentTitle", AttachmentDescription = "", AttachmentSiteID = CMSContext.CurrentSiteID, AttachmentGUID = Guid.NewGuid() };

Then you can call AddAttachment() method with following parameters:
AddAttachment( TreeNode node, string guidColumnName, Guid attachmentGuid, Guid groupGuid, Object file, TreeProvider tree, int width, int height, int maxSideSize )

Where Object file is your byte array of the image.
-fl-


See also: API Reference documentation

Applies to: Kentico 5.x, 6.x & 7.x
Share this article on   LinkedIn