Paul Matos
Paul Matos

Reputation: 169

Getting contents of Media Library Item in Sitecore

Using Sitecore 7.5, I am trying to store several html files inside of the Media Library. Then in my sublayout codebehind I am attempting to grab the inner content of those html files.

I had this working when I was storing the html file on the server. I would upload the file into the Media Library using 'upload as file', and then use the following code to read the content:

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

However I now would like to do this without storing the files on the server and instead only have them inside the media library. Is there anyway I can do this?

So far I have had a hard time trying to find information on the subject.

Thank you.

Upvotes: 2

Views: 5955

Answers (1)

Marek Musielak
Marek Musielak

Reputation: 27132

From what I understand you want to read content of an html file stored in Media Library.

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();
}

Upvotes: 8

Related Questions