簡體   English   中英

以編程方式將文件添加到 Kentico 媒體庫

[英]programmatically adding files to the Kentico Media Library

使用 CMSDesk 並單擊工具選項卡,然后單擊媒體庫,我可以將文件添加到內置的 Kentico 媒體庫。 有沒有辦法使用他們的 API 做到這一點?

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

[編輯:截至 2013 年 4 月 4 日,以上鏈接已失效。 如果有人找到替代鏈接,請更新並刪除此消息。]

您可以使用 Kentico API 執行此操作。 它實際上非常豐富,但那里的文檔和示例有點缺乏。

以下是一個示例方法(實際上用作 web 服務方法,因為我們有使用它的遠程和本地頁面)和一個調用它的示例方法(例如來自“編輯”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);
        }

將其保留在此處,因為原始鏈接似乎已失效。

發表於 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 object,在其中設置一些內容,然后將其插入 MediaFileInfoProvider。 MediaFileInfo object 中還有很多附加屬性,例如 FileSize,它(正如屬性名稱所暗示的那樣)以 long 形式存儲文件大小。 專業提示 – 使用CMS.GlobalHelper.DataHelper.GetSizeString function 將 long 轉換為字符串,將其格式化為用戶可讀的數據。

這實際上只是觸及了您可以在代碼隱藏中使用媒體庫做什么的表面。 仔細查看MediaFileInfoMediaFIleInfoProvider類,以及MediaLibraryHelperMediaLibraryInfoMediaLibraryInfoProvider類。 做不到的事情很少。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM