2015-10-16 90 views
2

使用Sitecore 7.5,我试图在媒体库中存储几个html文件。然后在我的sublayout代码隐藏中,我试图抓住这些html文件的内部内容。在Sitecore中获取媒体库项目的内容

我有这个工作,当我在服务器上存储的HTML文件。我会上传使用“上传的文件”的文件到媒体库中,然后使用下面的代码阅读的内容:

string filename = htmlMediaItem.Fields["File Path"].ToString(); 
string path = Server.MapPath(filename); 
string content = System.IO.File.ReadAllText(path); 

但是我现在想做到这一点不存储在服务器上的文件和而只能将它们放在媒体库中。无论如何,我可以做到这一点?

到目前为止,我已经有一个很难试图找到这方面的信息。

谢谢。

回答

7

从我知道你想读取存储在媒体库的HTML文件的内容。

Sitecore.Data.Items.Item sampleItem = Sitecore.Context.Database.GetItem("/sitecore/media library/Files/yourhtmlfile"); 
Sitecore.Data.Items.Item sampleMedia = new Sitecore.Data.Items.MediaItem(sampleItem); 
using(var reader = new StreamReader(MediaManager.GetMedia(sampleMedia).GetStream().Stream)) 
{ 
    string text = reader.ReadToEnd(); 
} 
+0

谢谢!!!!完美工作 –

相关问题