2011-07-21 78 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類。沒有什麼是不能做的。

相關問題