【发布时间】:2011-07-21 03:53:14
【问题描述】:
使用 CMSDesk 并单击工具选项卡,然后单击媒体库,我可以将文件添加到内置的 Kentico 媒体库。有没有办法使用他们的 API 来做到这一点?
【问题讨论】:
使用 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);
}
【讨论】:
把它留在这里,原来的 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 转换为字符串,将其格式化为用户可读的数据。
这实际上只是初步了解了您可以在代码隐藏中使用媒体库做什么。仔细查看MediaFileInfo 和MediaFIleInfoProvider 类,以及MediaLibraryHelper、MediaLibraryInfo、MediaLibraryInfoProvider 类。没有什么是做不到的。
【讨论】: