Sitecore:GetMediaStream始终为null [英] Sitecore: GetMediaStream is always null

查看:106
本文介绍了Sitecore:GetMediaStream始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件存储在我的主数据库和Web数据库(Sitecore)中. 我需要阅读所选文件的内容. 这是我使用的代码:

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始终为空! 我已经尝试了几个文件. 如果我尝试下载文件,则它们确实可以成功下载.

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.

这可能是什么原因?

我注意到web.comfig文件中也存在以下条目:

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.

推荐答案

您需要获取Sitecore项,然后再从中获取媒体项. Sitecore项和media项是两个不同的事物.您可以从代表它的Sitecore项中获取媒体项.

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.

请记住,sitecore项是模板,其中包含所有类型,大小,名称等字段.媒体项是实际内容,jpeg,pdf等.

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

这篇关于Sitecore:GetMediaStream始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆