2016-04-14 3 views
1

Мы используем Kentico v9 и импортируем сообщения из Wordpress в Kentico. У нас проблема с вложением вложений в сообщение в блоге. Вот код используется в данный момент:AddAttachment Method in Kentico

var attachmentNode = TreeNode.New(FILE_PAGE_TYPE); 
attachmentNode.DocumentName = filename; 
attachmentNode.DocumentCulture = postNode.DocumentCulture; 
attachmentNode.Insert(postNode); 
DocumentHelper.AddAttachment(attachmentNode, "ce4c5d10-c143-4ada-9d8a-7e7481b167ef", localFileLocation, postNode.TreeProvider); 
attachmentNode.Update(); 

Это не дает какой-либо ошибки, и есть запись в базе данных для файла. Однако сам файл не находится в Kentico. Кто-нибудь знает, что я здесь делаю неправильно?

ответ

0

См. answer I provided в Kentico DevNet.

 TreeNode newNode = TreeNode.New(PageType, tree); 
     newNode.DocumentName = mediaFile.FileName; 
     newNode.DocumentName = fileName.Trim(); 
     newNode.SetValue("FileDate", fileDate); 
     DocumentHelper.InsertDocument(newNode, parentNode.NodeID); 

     AttachmentInfo newAttachment = null; 

     // Path to the file to be inserted. This example uses an explicitly defined file path. However, you can use an object of the HttpPostedFile type (uploaded via an upload control). 
     string filePath = MediaLibraryPath + @"\" + mediaFile.FileName + mediaFile.FileExtension; 

     // Insert the attachment and update the document with its GUID 
     newAttachment = DocumentHelper.AddUnsortedAttachment(newNode, Guid.NewGuid(), filePath, tree, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE); 

     // attach the new attachment to the page/document 
     newNode.SetValue("FileAttachment", newAttachment.AttachmentGUID); 
     DocumentHelper.UpdateDocument(newNode); 
     newNode.Publish(); 
0

Я не думаю, что этот код выполняет любую операцию/обработку фактического файла - он просто сохраняет ссылку на этот файл в базе данных. Если вы хотите сохранить его в определенном месте в вашей файловой системе, вам необходимо реализовать соответствующие функции. См. Информацию о приложении API examples.