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

发布于 2024-11-25 09:09:52 字数 86 浏览 2 评论 0原文

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

Using CMSDesk and click on the Tools tab, then Media Library I can add files to the inbuilt Kentico Media Library. Is there a way to do this using their API?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话已封尘 2024-12-02 09:09:52

您可以使用 Kentico API 来完成此操作。它实际上非常丰富,但文档和示例有点缺乏。

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

文件徽标->受保护的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);
        }

You can do this using the Kentico API. It is actually quite rich but the documentation and samples out there are a bit lacking.

Following is a sample method (actually used as a web service method as we have both remote and local pages that use it) and a sample method that calls it (say from an 'edit' web page).

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);
        }
南渊 2024-12-02 09:09:52

将其保留为原始版本链接 似乎已失效。

发表于 2010 年 6 月 23 日,作者:kevin

那么,如果您曾经使用过基于 .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 类。没有什么是做不到的。

Keeping it here as the original link seems to be dead.

Posted on June 23, 2010 by kevin

So, if you’ve ever worked with the .NET based CMS Kentico (http://www.kentico.com), you’ll know that Media Libraries can be a very powerful tool for organizing your non-site data, including images, documents, and anything else that you need storing and integrating with your CMS. And it all works fantastically, as long as you don’t try to do anything with it code-side. That’s where things get interesting, to say the least.

The Kentico documentation website (http://devnet.kentico.com/documentation.aspx) is very useful in terms of working with and manipulating the tree from the code, it offers very little in terms of manipulating and working-with in general with Media Libraries. So I spent a good amount of time looking through the Modules seeing what Kentico does and how it does it so you don’t have to.

Since this is my first post, and I’m still a little rusty on the whole “writing” thing, so let’s just get to the code.

//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);

I think this is pretty self explanatory, we create a MediaFileInfo object, set some stuff in it, and then insert it into the MediaFileInfoProvider. There are a lot of additional properties within the MediaFileInfo object, such as FileSize, which (as the name of the property suggests), stores the file size in a long. Pro tip – use the CMS.GlobalHelper.DataHelper.GetSizeString function to convert the long to a string, formatting it into user-readable data.

This really just scratches the surface on what you can do with Media Libraries in the code-behind. Take a closer look at the MediaFileInfo and MediaFIleInfoProvider classes, along with the MediaLibraryHelper, MediaLibraryInfo, MediaLibraryInfoProvider classes. There’s very little that can’t be done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文