我们使用Kentico v9,并将Wordpress的博客文章导入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中。有人知道我在这里做错了什么吗?
请参阅我在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();
我不认为这段代码对实际文件进行任何操作/处理——它只是将对该文件的引用保存到数据库中。如果要将其保存在文件系统中的特定位置,则必须实现适当的功能。请参阅API使用附件的一些示例。