Sitecore: GetMediaStream is always null

岁酱吖の 提交于 2019-12-11 12:18:27

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/35616140/sitecore-getmediastream-is-always-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!