本文介绍了如何让 Yii sendFile 函数将 Content-Desposition 设置为内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Yii::app()->request->sendFile() 在视图中显示图像而不弹出下载对话框",但我是无法这样做,因为 sendFile 正在将 Content-Desposition 设置为 attachment.如何将其设置为 inline ?

I am trying to use Yii::app()->request->sendFile() to display images in the view without the 'Download Dialog box' popping up, but I am unable to do so as sendFile is setting Content-Desposition to attachment. How do I make it set it to inline ?

顺便说一句,我使用过 xsendfile 函数,但由于您需要在 Apache 中使用 mod-xsendfile 来使用它,因此放弃了它.

BTW I have used xsendfile function, but ditched it as you need mod-xsendfile in Apache to use it.

请帮忙.

推荐答案

如果要在视图中显示图像,则需要使用 标签.

If you want to display images in the view then you need to use <img> tags.

如果您希望返回的内容成为一个图像(在这种情况下谈论视图是没有意义的)那么您需要做的通常不超过

If you want the returned content to be an image (in which case it's meaningless to talk about a view) then all you need to do is usually no more than

header('Content-Type: image/jpeg');
echo $binaryImageContents;
Yii::app()->end();

这也是 sendFile() 原则上所做的,尽管它提供了更多的便利,因为它从文件的扩展名中检测适当的 MIME 类型并发送一堆额外的 HTTP 响应头(包括不受欢迎的 Content-Disposition 一).你可以看看为自己.

That's also what sendFile() does in principle, although it offers a bit more convenience because it detects the appropriate MIME type from the file's extension and sends a bunch of additional HTTP response headers (including the undesirable Content-Disposition one). You can take a look for yourself.

这篇关于如何让 Yii sendFile 函数将 Content-Desposition 设置为内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:08