Kentico 11-MediaFileInfoProvider.DeleteMediaFileInfo不删除

时间:2019-01-31 16:19:49

标签: kentico media-library

MediaFileInfo updateFile = MediaFileInfoProvider.GetMediaFileInfo(library.LibraryID,file.Name);

上面的代码行并未按预期从媒体库中删除文件。这是针对Kentico 11 MVC中的计划任务。确实创建了新文件,并且系统已将其重命名以防止冲突。 我想在导入文件的更新版本之前删除现有文件。如果新文件覆盖了现有文件,我什至会感到满意。

public void UpdateMediaFile(MediaLibraryInfo库,字符串fileName,字符串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

            }
        }
    }

2 个答案:

答案 0 :(得分:0)

我将添加日志记录以确保您的代码被命中。确保updatefile不为null。我认为您必须传递媒体库中的文件路径,而不仅仅是名称。

答案 1 :(得分:0)

如果我没记错的话,MediaFileInfoProvider.DeleteMediaFileInfo将从数据库中删除一条记录,但不会物理删除该文件,因此您需要调用CMS.IO.FileInfo.Delete(filePath)从磁盘中删除它。