Thank you very much Brian for your help.
I have updated my code accordingly to make it work with binary:
byte[] fileContentBytes = file.GetContentBytes();
DocumentAttachment attachment = DocumentHelper.GetAttachment(page, file.Name);
//If no attachment with the same name, we add a new one
if (attachment == null)
{
using (var fs = new FileStream(targetFilePath, FileMode.Create, FileAccess.Write))
{
fs.Write(fileContentBytes, 0, fileContentBytes.Length);
}
attachment = DocumentHelper.AddAttachment(page, "DocFile", targetFilePath);
attachment.AttachmentName = file.Name;
attachment.AttachmentTitle = file.Title;
attachment.Update();
// File after being saved in the Temp folder, loaded into Kentico, is now deleted as there is a copy of it named according to its GUID in the DB
System.IO.File.Delete(targetFilePath);
}
//If attachment already exists, we update it so we keep the GUID which could be used by other pages
else
{
DocumentHelper.UpdateAttachment(page, attachment);
//We update the binary
attachment.AttachmentBinary = fileContentBytes;
attachment.AttachmentName = file.Name;
attachment.AttachmentTitle = file.Title;
attachment.Update();
}
In my case, the file comes from SharePoint, do I need to save it first as a file in order to create the attachment in the DocumentHelper.AddAttachment
or can I use the binary directly from file.GetContentBytes()
as I did for the update section?
Thank you
Sylvain