简体   繁体   中英

programmatically adding files to the Kentico Media Library

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?

this seems to do what you want http://www.rustedmushroom.com/2010/06/working-with-media-libraries-in-kentico-undocumented-api-style/

[EDIT: As of 4/4/2013, this link above is dead. If someone finds an alternate link, please update and remove this message.]

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM