Anthony Fernandes
Anthony Fernandes

Reputation: 611

Sitecore: GetMediaStream is always null

I have files stored in my master and web databases (Sitecore). I need to read the content of the a selected file. This is the code I use:

MediaItem mediaItem = Sitecore.Context.Database.Items.GetItem(id);  
if (mediaItem != null)  
{  
    Stream stream = mediaItem.GetMediaStream();  
}

stream is always null! I've tried this for several files. If I try to download the files, they do download successfully.

I need to read the contents of the file. The mediaitem returns all the meta data (file name, extension etc) correctly.

What could be the reason for this?

I noticed that the following entry is present in the web.comfig file as well:

      <getMediaStream>
        <processor type="Sitecore.Resources.Media.ThumbnailProcessor, Sitecore.Kernel"/>
        <processor type="Sitecore.Resources.Media.ResizeProcessor, Sitecore.Kernel"/>
    <processor type="Sitecore.Resources.Media.GrayscaleProcessor, Sitecore.Kernel"/>
  </getMediaStream>

I'm not sure if I need to add anything here.

Upvotes: 2

Views: 1154

Answers (1)

Chris Auer
Chris Auer

Reputation: 1445

You need to get the Sitecore item and then get the media item out of it. The Sitecore item and the media item are two different things. And you can get the media item from the Sitecore item that represents it.

Remembering that the sitecore item is the template with all the fields like type, size, name, etc. The media item is the actual thing, jpeg, pdf, etc.

Item item = Sitecore.Context.Database.Items.GetItem(id); 
MediaItem mediaItem = new MediaItem(item); 

if (mediaItem != null)  
{  
    Stream stream = mediaItem.GetMediaStream();  
}

Upvotes: 1

Related Questions