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

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

您可以使用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); } 

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

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

将它保存在这里作为原始链接似乎已经死了。

由凯文于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类。 很少有人无法做到。