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

C# 以编程方式将文件添加到Kentico媒体库,c#,asp.net,kentico,C#,Asp.net,Kentico,使用CMSDesk并单击“工具”选项卡,然后我可以将文件添加到内置的Kentico媒体库中。有没有一种方法可以使用他们的API实现这一点?您可以使用Kentico API实现这一点。它实际上非常丰富,但是文档和样本有点缺乏 下面是一个示例方法(实际上用作web服务方法,因为我们有使用它的远程和本地页面)和一个调用它的示例方法(比如从“编辑”web页面) fileLogo->protected System.Web.UI.WebControls.FileUpload fileLogo

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

您可以使用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);
        }
[WebMethod]
公共bool导入(int libraryID、字符串folderName、字符串文件名、字节[]字节)
{
SiteInfo SiteInfo=SiteInfoProvider.GetCurrentSite();
MediaLibraryInfo-libraryInfo=MediaLibraryInfo提供程序。GetMediaLibraryInfo(libraryID);
fileName=fileName.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));
writealBytes(文件路径,字节);
if(File.Exists(filePath))
{
字符串路径=MediaLibraryHelper.EnsurePath(文件路径);
MediaFileInfo fileInfo=新的MediaFileInfo(文件路径,libraryInfo.LibraryID,folderName);
fileInfo.FileSiteID=siteInfo.SiteID;
MediaFileInfoProvider.ImportMediaFileInfo(fileInfo);
bRetValue=true;
}
返回bRetValue;
}
字符串filePath=“~/SITENAME/media/SITE_MEDIALIB/Logos/”;
字符串文件名=string.Empty;
如果(fileLogo.FileName.Length>0)
{
var ext=fileLogo.FileName.Substring(fileLogo.FileName.LastIndexOf('.')+1.ToLower();
fileName=entryTitle+“+ext;
MediaLibrary il=新的MediaLibrary();
导入(3,“FOLDERNAME”,文件名,fileLogo.FileBytes);
}

将它保留在这里,因为原作似乎已死亡。

凯文于2010年6月23日发布

那么,如果您曾经使用过基于.NET的CMS Kentico(),你会知道媒体库是一个非常强大的工具,可以用来组织你的非站点数据,包括图像、文档,以及你需要存储并与你的CMS集成的任何其他数据。而且,只要你不尝试在代码方面做任何事,它的工作都非常出色。至少,这就是事情变得有趣的地方t

Kentico文档网站()在处理和操作代码中的树方面非常有用,在处理和使用媒体库方面,它提供的帮助非常少。因此,我花了大量时间浏览了模块,了解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.Pro提示中–使用
CMS.GlobalHelper.DataHelper.GetSizeString
函数将long转换为字符串,并将其格式化为用户可读的数据

这实际上只是在代码背后对媒体库所能做的事情的皮毛。请仔细查看
MediaFileInfo
MediaFIleInfoProvider
类,以及
MediaLibraryHelper
MediaLibraryInfo
MediaLibraryInfo
类。其中那是办不到的