c# - 在 Sitecore 中获取媒体库项目的内容

标签 c# sitecore sitecore7.5

使用 Sitecore 7.5,我试图在媒体库中存储几个 html 文件。然后在我的子布局代码隐藏中,我试图获取那些 html 文件的内部内容。

当我将 html 文件存储在服务器上时,我有这个工作。我会使用“上传为文件”将文件上传到媒体库,然后使用以下代码读取内容:

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

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

到目前为止,我很难找到有关该主题的信息。

谢谢。

最佳答案

据我了解,您想阅读存储在媒体库中的 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();
}

关于c# - 在 Sitecore 中获取媒体库项目的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33173335/

相关文章:

c# - 如何使用Solr Provider在Sitecore ContentSearch中控制嵌套查询的优先级?

indexing - 如何向 Sitecore 实现的 Solr 索引中的 _content 字段添加文本?

c# - 浏览 Entity Framework 迁移选项

c# - java是否有类似于C#属性的东西?

sitecore - 在 Sitecore 中使用 ItemRendering 将参数传递给渲染

c# - 生产中的 Sitecore 自定义 404 处理程序

c# - 将默认用户个人资料图片设置为其姓名首字母的图像

c# - 将 Base64 值转换为数字

xml - 使用 XPath/Sitecore 查询获取层次结构中特定级别的祖先

javascript - 从 JavaScript (MVC) 调用 Sitecore Controller 方法