C# 使用c重命名Sitecore媒体项#
我想重命名或给上传的媒体项目命名,但它总是以未命名的项目(媒体)名称上传。下面是我的代码C# 使用c重命名Sitecore媒体项#,c#,asp.net,sitecore,C#,Asp.net,Sitecore,我想重命名或给上传的媒体项目命名,但它总是以未命名的项目(媒体)名称上传。下面是我的代码 public static void UploadImage(int customerID,FileUpload fup) { using (new SecurityDisabler()) { var options = new Sitecore.Resources.Media.MediaCreatorOptions
public static void UploadImage(int customerID,FileUpload fup)
{
using (new SecurityDisabler())
{
var options = new Sitecore.Resources.Media.MediaCreatorOptions
{
AlternateText =customerID.ToString(),
FileBased = false,
IncludeExtensionInItemName = false,
KeepExisting = false,
Versioned = false,
Destination = "/sitecore/media library/temp",
Database = Sitecore.Configuration.Factory.GetDatabase("master")
};
var filepath = HttpContext.Current.Server.MapPath(fup.FileName);
var creator = new MediaCreator();
var mediaItem = creator.CreateFromStream(fup.PostedFile.InputStream, filepath, options);
MediaItem myFile = mediaItem;
myFile.Name = customerID.ToString(); // unable to give becasue it read only
}
}
重命名前尝试调用
.InnerItem.Editing.BeginEdit()
,重命名后尝试调用.InnerItem.Editing.EndEdit()
:
myFile.InnerItem.Editing.BeginEdit();
myFile.InnerItem.Name = customerID.ToString();
myFile.InnerItem.Editing.EndEdit();
有关更多信息,请参见章节
3.1.5如何将项目置于编辑模式
此处我已尝试过。这会导致编译错误。因为它显示只读。请参阅编辑的答案。也许.InnerItem
适合您。