【问题标题】:Getting contents of Media Library Item in Sitecore在 Sitecore 中获取媒体库项目的内容
【发布时间】:2015-10-16 23:32:10
【问题描述】:

使用 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);

但是,我现在想在不将文件存储在服务器上的情况下执行此操作,而只将它们保存在媒体库中。无论如何我可以做到这一点吗?

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

谢谢。

【问题讨论】:

    标签: c# sitecore sitecore7.5


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多