Importing Image into File Field Type

Andre Pfanz asked on June 6, 2016 23:11

We are trying to upload several images into existing pages in Kentico v9 using an import program written by us. For each image, we are creating a new node of a custom page type. One of the fields in this custom page type is a data type of "File" called "UploadedImage." I have the images saved locally on my computer and have a console program to import them. The import program first creates the node to store the image using the custom page type. I then try to upload the image from my local computer to the Kentico server. Here's what I have:

var attachmentNode = TreeNode.New(FILE_PAGE_TYPE);
attachmentNode.DocumentName = Path.GetFileName(imagePath);
attachmentNode.DocumentCulture = galleryNode.DocumentCulture;
//Assign the filename
attachmentNode.SetValue("FileTitle", Path.GetFileName(imagePath));
DocumentHelper.InsertDocument(attachmentNode, galleryNode);
//Save the file into the attachment
AttachmentInfo attachmentInfo = DocumentHelper.AddUnsortedAttachment(attachmentNode, Guid.NewGuid(), imagePath, new TreeProvider());
attachmentNode.SetValue("UploadedImage", attachmentInfo.AttachmentBinary);
//Publish the attachment node

My problem is nothing is being loaded into the File field. I appreciate any help anyone can give to get the binary data into the File field.

Recent Answers

Brenden Kehren answered on June 7, 2016 02:52 (last edited on June 7, 2016 02:53)

If you're adding an attachment to a document, it will NOT be a binary field, it will be a GUID that is referencing an attachment object which will store the binary data. Try something like this after your DocumentHelper.InsertDocument call:

AttachmentInfo newAttachment = DocumentHelper.AddUnsortedAttachment(newNode, Guid.NewGuid(), filePath, tree, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE);  
newNode.SetValue("FileAttachment", newAttachment.AttachmentGUID);
1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.