以编程方式将文件添加到Kentico媒体库

时间:2011-07-21 03:53:15

标签: c# asp.net kentico

使用CMSDesk并单击工具选项卡,然后媒体库我可以将文件添加到内置的Kentico媒体库。有没有办法使用他们的API来做到这一点?

3 个答案:

答案 0 :(得分:4)

这似乎做你想要的 http://www.rustedmushroom.com/2010/06/working-with-media-libraries-in-kentico-undocumented-api-style/

[编辑:截至2013年4月4日,上面的链接已经死了。如果有人找到了备用链接,请更新并删除此消息。]

答案 1 :(得分:4)

您可以使用Kentico API执行此操作。它实际上相当丰富,但文档和样本有点缺乏。

以下是一个示例方法(实际上用作Web服务方法,因为我们有使用它的远程和本地页面)和一个调用它的示例方法(例如从“编辑”网页)。

fileLogo - > protected System.Web.UI.WebControls.FileUpload fileLogo;

        [WebMethod]
    public bool Import(int libraryID, string folderName, string fileName, byte[] bytes)
    {
        SiteInfo siteInfo = SiteInfoProvider.GetCurrentSite();
        MediaLibraryInfo libraryInfo = MediaLibraryInfoProvider.GetMediaLibraryInfo(libraryID);

        fileName = fileName.Replace(" ", "-").Replace("&", "-").Replace("'", "-").Replace("+", "-").Replace("=", "-").Replace("[", "-").Replace("]", "-").Replace("#", "-").Replace("%", "-").Replace("\\", "-").Replace("/", "-").Replace(":", "-").Replace("*", "-").Replace("?", "-").Replace("\"", "-").Replace("<", "-").Replace(">", "-").Replace("|", "-");

        bool bRetValue = false;

        string filePath = Server.MapPath(string.Format("/{0}/media/{1}/{2}/{3}", siteInfo.SiteName, libraryInfo.LibraryFolder, folderName, fileName));
        File.WriteAllBytes(filePath, bytes);
        if (File.Exists(filePath))
        {
            string path = MediaLibraryHelper.EnsurePath(filePath);
            MediaFileInfo fileInfo = new MediaFileInfo(filePath, libraryInfo.LibraryID, folderName);
            fileInfo.FileSiteID = siteInfo.SiteID;
            MediaFileInfoProvider.ImportMediaFileInfo(fileInfo);
            bRetValue = true;
        }

        return bRetValue;
    }

            string filePath = "~/SITENAME/media/SITE_MEDIALIB/Logos/";
        string fileName = string.Empty  ;

        if (fileLogo.FileName.Length > 0)
        {
            var ext = fileLogo.FileName.Substring(fileLogo.FileName.LastIndexOf('.') + 1).ToLower();

            fileName = entryTitle + "." + ext; 

            MediaLibrary il = new MediaLibrary();
            il.Import(3, "FOLDERNAME", fileName, fileLogo.FileBytes);
        }

答案 2 :(得分:0)

保持原状link似乎已经死了。

2010年6月23日由凯文发表。

所以,如果你曾经使用过基于.NET的CMS Kentico(http://www.kentico.com),你就会知道媒体库可以成为一个非常强大的工具来组织你的非站点数据,包括图像,文档以及您需要存储和集成CMS的任何其他内容。只要你不尝试用代码方做任何事情,这一切都很有效。至少可以说,这就是事情变得有趣的地方。

Kentico文档网站(http://devnet.kentico.com/documentation.aspx)在从代码中处理和操作树方面非常有用,它在操作和处理媒体库方面提供的很少。因此,我花了很多时间查看模块,了解Kentico的作用以及它是如何做到的,所以你没必要这样做。

由于这是我的第一篇文章,而且我对整个“写作”事情仍然有点生疏,所以让我们来看看代码。

//Media Library Info - takes Media Library Name and Website Name
MediaLibraryInfo libraryInfo = MediaLibraryInfoProvider.GetMediaLibraryInfo("Website", "MediaLibrary");
//Folder in Media Library where Item will be Inserted
string mediaLibraryFolder = "MediaLibraryFolder";
//Absolute Path to File
string filePath = Server.MapPath("~/Website/media/MediaLibrary/" + "MediaLibraryFolder/MediaLibraryItem.pdf");
// Get Relative Path to File
string path = MediaLibraryHelper.EnsurePath(filePath);
//create media file info item - takes the relative path to the document, the library ID, and the folder name where the document will be located within the media library
MediaFileInfo fileInfo = new MediaFileInfo(path, libraryInfo.LibraryID, mediaLibraryFolder);
//set the title to something nice
fileInfo.FileTitle = "Document Title";
//set the description to something useful
fileInfo.FileDescription = "Document Description";
// Save media file info
MediaFileInfoProvider.ImportMediaFileInfo(fileInfo);

我认为这是非常自我解释的,我们创建一个MediaFileInfo对象,在其中设置一些内容,然后将其插入MediaFileInfoProvider。 MediaFileInfo对象中有许多其他属性,例如FileSize,(如属性名称所示),将文件大小存储为long。专业提示 - 使用CMS.GlobalHelper.DataHelper.GetSizeString函数将long转换为字符串,将其格式化为用户可读数据。

这实际上只是在代码隐藏中使用媒体库可以做些什么。仔细查看MediaFileInfoMediaFIleInfoProvider类,以及MediaLibraryHelperMediaLibraryInfoMediaLibraryInfoProvider类。很少有人无法做到。