Kentico 11 - MediaFileInfoProvider.DeleteMediaFileInfo 不删除
Kentico 11 - MediaFileInfoProvider.DeleteMediaFileInfo Not Deleting
MediaFileInfo updateFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, file.Name);
上面的代码行没有像我预期的那样从媒体库中删除文件。这是针对 Kentico 11 MVC 中的计划任务。新文件确实已创建并由系统重命名以防止冲突。
我想在导入文件的更新版本之前删除现有文件。如果新文件覆盖现有文件,我什至会感到满意。
public void UpdateMediaFile(MediaLibraryInfo 库,字符串文件名,字符串 importPath)
{
//LumberMarketReport.pdf 和 PanelMarketReport.pdf
if (library != null)
{
// Prepares a path to a local file
string filePath = fileName;
// Prepares a CMS.IO.FileInfo object representing the local file
CMS.IO.FileInfo file = CMS.IO.FileInfo.New(filePath);
if (file != null)
{
#region "Delete Existing"
MediaFileInfo updateFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, file.Name);
if (updateFile != null)
{
MediaFileInfoProvider.DeleteMediaFileInfo(updateFile);
}
#endregion
#region "Create File"
// Creates a new media library file object
MediaFileInfo mediaFile = new MediaFileInfo(filePath, library.LibraryID);
// Sets the media library file properties
mediaFile.FileName = file.Name;
mediaFile.FileDescription = "This file was added through the API.";
mediaFile.FilePath = "/"; // Sets the path within the media library's folder structure
mediaFile.FileExtension = file.Extension;
mediaFile.FileMimeType = MimeTypeHelper.GetMimetype(file.Extension);
mediaFile.FileSiteID = SiteContext.CurrentSiteID;
mediaFile.FileLibraryID = library.LibraryID;
mediaFile.FileSize = file.Length;
if (file.Name == "PanelMarketReport.pdf")
{
mediaFile.FileTitle = "Panel Market Report";
mediaFile.SetValue("FileCategoryID", 19);
}
else if (file.Name == "LumberMarketReport.pdf")
{
mediaFile.FileTitle = "Lumber Market Report";
mediaFile.SetValue("FileCategoryID", 57);
}
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(mediaFile);
#endregion
}
}
}
我会添加日志记录以确保您的代码被命中。确保更新文件不为空。我认为您必须传入媒体库中的文件路径,而不仅仅是名称。
如果我没记错的话MediaFileInfoProvider.DeleteMediaFileInfo
将从数据库中删除一条记录但不会物理删除文件,因此您需要调用CMS.IO.FileInfo.Delete(filePath)
从磁盘中删除它。
MediaFileInfo updateFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, file.Name);
上面的代码行没有像我预期的那样从媒体库中删除文件。这是针对 Kentico 11 MVC 中的计划任务。新文件确实已创建并由系统重命名以防止冲突。 我想在导入文件的更新版本之前删除现有文件。如果新文件覆盖现有文件,我什至会感到满意。
public void UpdateMediaFile(MediaLibraryInfo 库,字符串文件名,字符串 importPath) { //LumberMarketReport.pdf 和 PanelMarketReport.pdf
if (library != null)
{
// Prepares a path to a local file
string filePath = fileName;
// Prepares a CMS.IO.FileInfo object representing the local file
CMS.IO.FileInfo file = CMS.IO.FileInfo.New(filePath);
if (file != null)
{
#region "Delete Existing"
MediaFileInfo updateFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID, file.Name);
if (updateFile != null)
{
MediaFileInfoProvider.DeleteMediaFileInfo(updateFile);
}
#endregion
#region "Create File"
// Creates a new media library file object
MediaFileInfo mediaFile = new MediaFileInfo(filePath, library.LibraryID);
// Sets the media library file properties
mediaFile.FileName = file.Name;
mediaFile.FileDescription = "This file was added through the API.";
mediaFile.FilePath = "/"; // Sets the path within the media library's folder structure
mediaFile.FileExtension = file.Extension;
mediaFile.FileMimeType = MimeTypeHelper.GetMimetype(file.Extension);
mediaFile.FileSiteID = SiteContext.CurrentSiteID;
mediaFile.FileLibraryID = library.LibraryID;
mediaFile.FileSize = file.Length;
if (file.Name == "PanelMarketReport.pdf")
{
mediaFile.FileTitle = "Panel Market Report";
mediaFile.SetValue("FileCategoryID", 19);
}
else if (file.Name == "LumberMarketReport.pdf")
{
mediaFile.FileTitle = "Lumber Market Report";
mediaFile.SetValue("FileCategoryID", 57);
}
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(mediaFile);
#endregion
}
}
}
我会添加日志记录以确保您的代码被命中。确保更新文件不为空。我认为您必须传入媒体库中的文件路径,而不仅仅是名称。
如果我没记错的话MediaFileInfoProvider.DeleteMediaFileInfo
将从数据库中删除一条记录但不会物理删除文件,因此您需要调用CMS.IO.FileInfo.Delete(filePath)
从磁盘中删除它。