2011-07-21 67 views

回答

4

您可以使用Kentico API执行此操作。它实际上非常丰富,但文档和样本比较缺乏。

以下是一个示例方法(实际上用作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); 
     } 
4

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

[编辑:截至2013年4月4日,这个环节上面已经死了。如果有人发现其他链接,请更新并删除此邮件。]

+0

好文章,从我+1。你写了这个吗?你有更多的文章吗? Kentico是一个非常强大的产品,但doco是可怕的... – Grimboify

+0

谢谢...但我没有写 - 只是发现它... – Yahia

0

这里保持它原来的link似乎是死了。

由Kevin发表于2010年6月23日,

所以,如果你曾经与基于.NET CMS Kentico(http://www.kentico.com),你就会知道这工作介质库可以成为一个非常强大的工具组织您的非网站数据,包括图片,文档以及其他您需要存储并与CMS集成的其他内容。而且这一切都非常有效,只要你不想在代码方面做任何事情。这就是事情变得有趣的地方,至少可以说。

Kentico文档网站(http://devnet.kentico.com/documentation.aspx)在代码工作和操作树方面非常有用,它在操作和工作方面提供的功能非常少,通常与Media Libraries相关。所以我花了很多时间浏览模块,看看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类,以及MediaLibraryHelper,MediaLibraryInfo,MediaLibraryInfoProvider类。没有什么是不能做的。

相关问题