Kentico CMS 在尝试通过 Kentico 覆盖现有文件时创建附加文件 API
Kentico CMS creating additional file when attempting to overwrite existing one via Kentico API
正如您在下面的屏幕截图中看到的那样,当尝试使用 Kentico API 11.0.26 覆盖 现有媒体文件时,Kentico CMS 正在创建一个附加文件。我的目标是只有一个文件。
这是我的 API 代码
// Gets the media library
MediaLibraryInfo library = MediaLibraryInfoProvider.GetMediaLibraryInfo(libraryName, siteName);
if (library != null)
{
// Gets all .png files from the "NewFolder" folder of the specified media library
var mediaFiles = MediaFileInfoProvider.GetMediaFiles()
.WhereEquals("FileLibraryID", library.LibraryID)
.WhereEquals("FileName", fileName)
.WhereStartsWith("FilePath", folderName);
if (mediaFiles.Count == 0)//no existing file by that name, create new one
{
// Creates a new media library file object
MediaFileInfo mediaFile = new MediaFileInfo();
// Sets the media library file properties
mediaFile.FileSiteID = siteId;
mediaFile.FileLibraryID = library.LibraryID;
mediaFile.FileName = fileName;
mediaFile.FileTitle = headerSubtitle;
mediaFile.FileDescription = "some report";
mediaFile.FilePath = string.Format("{0}/", folderName); // Sets the path within the media library's folder structure
mediaFile.FileExtension = ".pdf";
mediaFile.FileMimeType = MimeTypeHelper.GetMimetype(".pdf");
using (StreamReader streamReader = new StreamReader(outputFilename))
{
mediaFile.FileBinaryStream = streamReader.BaseStream;
mediaFile.FileSize = streamReader.BaseStream.Length;
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(mediaFile);
}
}
else//file already existing by that name, update it's contents
{
// Gets the media file
MediaFileInfo updateFile = mediaFiles.FirstObject;
using (StreamReader streamReader = new StreamReader(outputFilename))
{
updateFile.FileBinaryStream = streamReader.BaseStream;
updateFile.FileSize = streamReader.BaseStream.Length;
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(updateFile);
}
}
}
尝试将 SetMediaFileInfo 方法的 ensureUniqueFileName 参数设置为 false,如下所示:MediaFileInfoProvider.SetMediaFileInfo(mediaFile, false);
。
如果这不起作用,您最好先删除现有文件。
也许 MediaFileInfoProvider.SetMediaFileInfo(mediaFile, false);
解决了这个问题,但在一个项目中我通过在保存新文件之前删除以前的文件解决了这个问题。
...
else //file already existing by that name, update it's contents
{
// Gets the media file
MediaFileInfo updateFile = mediaFiles.FirstObject;
updateFile.Delete(); // DELETE IT
using (StreamReader streamReader = new StreamReader(outputFilename))
{
MediaFileInfo updateFile = new MediaFileInfo();
//Set here all properties
updateFile.FileBinaryStream = streamReader.BaseStream;
updateFile.FileSize = streamReader.BaseStream.Length;
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(updateFile);
}
}
正如您在下面的屏幕截图中看到的那样,当尝试使用 Kentico API 11.0.26 覆盖 现有媒体文件时,Kentico CMS 正在创建一个附加文件。我的目标是只有一个文件。
这是我的 API 代码
// Gets the media library
MediaLibraryInfo library = MediaLibraryInfoProvider.GetMediaLibraryInfo(libraryName, siteName);
if (library != null)
{
// Gets all .png files from the "NewFolder" folder of the specified media library
var mediaFiles = MediaFileInfoProvider.GetMediaFiles()
.WhereEquals("FileLibraryID", library.LibraryID)
.WhereEquals("FileName", fileName)
.WhereStartsWith("FilePath", folderName);
if (mediaFiles.Count == 0)//no existing file by that name, create new one
{
// Creates a new media library file object
MediaFileInfo mediaFile = new MediaFileInfo();
// Sets the media library file properties
mediaFile.FileSiteID = siteId;
mediaFile.FileLibraryID = library.LibraryID;
mediaFile.FileName = fileName;
mediaFile.FileTitle = headerSubtitle;
mediaFile.FileDescription = "some report";
mediaFile.FilePath = string.Format("{0}/", folderName); // Sets the path within the media library's folder structure
mediaFile.FileExtension = ".pdf";
mediaFile.FileMimeType = MimeTypeHelper.GetMimetype(".pdf");
using (StreamReader streamReader = new StreamReader(outputFilename))
{
mediaFile.FileBinaryStream = streamReader.BaseStream;
mediaFile.FileSize = streamReader.BaseStream.Length;
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(mediaFile);
}
}
else//file already existing by that name, update it's contents
{
// Gets the media file
MediaFileInfo updateFile = mediaFiles.FirstObject;
using (StreamReader streamReader = new StreamReader(outputFilename))
{
updateFile.FileBinaryStream = streamReader.BaseStream;
updateFile.FileSize = streamReader.BaseStream.Length;
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(updateFile);
}
}
}
尝试将 SetMediaFileInfo 方法的 ensureUniqueFileName 参数设置为 false,如下所示:MediaFileInfoProvider.SetMediaFileInfo(mediaFile, false);
。
如果这不起作用,您最好先删除现有文件。
也许 MediaFileInfoProvider.SetMediaFileInfo(mediaFile, false);
解决了这个问题,但在一个项目中我通过在保存新文件之前删除以前的文件解决了这个问题。
...
else //file already existing by that name, update it's contents
{
// Gets the media file
MediaFileInfo updateFile = mediaFiles.FirstObject;
updateFile.Delete(); // DELETE IT
using (StreamReader streamReader = new StreamReader(outputFilename))
{
MediaFileInfo updateFile = new MediaFileInfo();
//Set here all properties
updateFile.FileBinaryStream = streamReader.BaseStream;
updateFile.FileSize = streamReader.BaseStream.Length;
// Saves the media library file
MediaFileInfoProvider.SetMediaFileInfo(updateFile);
}
}