Kentico 从上载的文件填充文档名称和标题
在内容树中手动添加新文件(cms.file)时,我希望能够使用上载文件的名称填充文档名称和标题。这可能吗?如果是这样的话,有谁能为我指出如何实施的正确方向。谢谢。您可以附加到例如Kentico 从上载的文件填充文档名称和标题,kentico,Kentico,在内容树中手动添加新文件(cms.file)时,我希望能够使用上载文件的名称填充文档名称和标题。这可能吗?如果是这样的话,有谁能为我指出如何实施的正确方向。谢谢。您可以附加到例如DocumentEvents.Insert.事件之前,检查页面类型是否为cms.file,并执行自定义操作。您可以了解有关处理全局事件的更多信息 您的代码可能类似于: using System; using CMS.Base; using CMS.DocumentEngine; [ChangeFileNameA
DocumentEvents.Insert.
事件之前,检查页面类型是否为cms.file
,并执行自定义操作。您可以了解有关处理全局事件的更多信息
您的代码可能类似于:
using System;
using CMS.Base;
using CMS.DocumentEngine;
[ChangeFileNameAttibute]
public partial class CMSModuleLoader
{
private class ChangeFileNameAttibute : CMSLoaderAttribute
{
public override void Init()
{
DocumentEvents.Insert.Before += Insert_Before;
}
void Insert_Before(object sender, DocumentEventArgs e)
{
if (e.Node.ClassName.ToLower() == "cms.file")
{
e.Node.doSomething(); // You can edit document here
}
}
}
}
这可以通过克隆用于在Kentico实例中创建新文件的页面来实现。将其克隆到您自己的目录并进行所需更改的最佳做法状态 我过去所做的是修改
ProcessFileUploader()
和ProcessDirectUploader()
方法,将值设置提取到单个函数中。然后更新上述方法,以使用新调用的方法设置这些重要值
新功能:
private void SetFileFieldValues(ref TreeNode node, string fileName)
{
int maxFileNameLength = FileNameFieldInfo.Size;
if (fileName.Length > maxFileNameLength)
{
fileName = fileName.Substring(0, maxFileNameLength);
}
node.DocumentName = fileName;
if (node.ContainsColumn("FileName"))
{
node.SetValue("FileName", fileName);
}
if (node.ContainsColumn("FileLongName"))
{
string fileLongName = txtFileLongName.Text.Trim();
fileLongName = TextHelper.LimitLength(fileLongName, txtFileLongName.MaxLength);
node.SetValue("FileLongName", fileLongName);
}
if (node.ContainsColumn("FileShortName"))
{
string fileShortName = txtFileShortName.Text.Trim();
fileShortName = TextHelper.LimitLength(fileShortName, txtFileShortName.MaxLength);
node.SetValue("FileShortName", fileShortName);
}
if (node.ContainsColumn("FileDescription"))
{
string fileDescription = txtFileDescription.Text.Trim();
fileDescription = TextHelper.LimitLength(fileDescription, txtFileDescription.MaxLength);
node.SetValue("FileDescription", fileDescription);
}
if(node.ContainsColumn("DocumentSearchExcluded"))
{
bool excludeFromSearch = ValidationHelper.GetBoolean(cbxExcludeFromSearch.Checked, false);
node.SetValue("DocumentSearchExcluded", excludeFromSearch);
}
}
更新的方法:
protected void ProcessFileUploader()
{
TreeNode node = DocumentManager.Node;
// Create new document
string fileName = Path.GetFileNameWithoutExtension(FileUpload.FileName);
SetFileFieldValues(ref node, fileName);
node.SetValue("FileAttachment", Guid.Empty);
// Set default template ID
node.SetDefaultPageTemplateID(DataClass.ClassDefaultPageTemplateID);
// Ensures documents consistency (blog post hierarchy etc.)
DocumentManager.EnsureDocumentsConsistency();
// Insert the document
DocumentHelper.InsertDocument(node, DocumentManager.ParentNode, DocumentManager.Tree);
// Add the file
DocumentHelper.AddAttachment(node, "FileAttachment", FileUpload.PostedFile, DocumentManager.Tree, ResizeToWidth, ResizeToHeight, ResizeToMaxSideSize);
}
另一个呢
protected void ProcessDirectUploader()
{
TreeNode node = DocumentManager.Node;
// Create new document
string fileName = Path.GetFileNameWithoutExtension(ucDirectUploader.AttachmentName);
SetFileFieldValues(ref node, fileName);
node.SetValue("FileAttachment", Guid.Empty);
// Set default template ID
node.SetDefaultPageTemplateID(DataClass.ClassDefaultPageTemplateID);
// Ensures documents consistency (blog post hierarchy etc.)
DocumentManager.EnsureDocumentsConsistency();
// Insert the document
DocumentHelper.InsertDocument(node, DocumentManager.ParentNode, DocumentManager.Tree);
// Set the attachment GUID later - important when document is under workflow and using check-in/check-out
node.SetValue("FileAttachment", ucDirectUploader.Value);
}
最后但并非最不重要的一点是,您需要更新文件页面类型(或创建自己的克隆),并在页面类型定义中添加新页面的文件位置
这可能不完全是你需要的,但应该会让你非常接近你需要做的事情。谢谢你的回答,但看起来这些事情发生得太晚了。为了清楚起见,我希望访问文件上传触发的事件(如果有)(在保存文档之前)