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

最佳答案

您可以使用Kentico API进行此操作。它实际上非常丰富,但是那里缺少文档和样本。

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

fileLogo->受保护的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);
        }

关于c# - 以编程方式将文件添加到Kentico媒体库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6771085/

10-10 19:30