gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:56:40 27 4
gpt4 key购买 nike

使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com