【问题标题】:Show an image using X-Send File (Symfony2)使用 X-Send File (Symfony2) 显示图像
【发布时间】:2014-10-08 10:41:01
【问题描述】:

我在“app/var/assets”中有一些图像(我必须将这些图像放在那个目录中,我无法通过客户端限制来更改它)。

我需要展示这些图片。 App 目录不是 Apache 的可访问路径,所以我需要使用 X-Send File。

我怎样才能使用 X-Send 文件呢?

我在我的控制器中尝试过:

$path = $this->get('kernel')->getRootDir() . '/var/assets/example.jpg';

$response = new BinaryFileResponse($path');

$response->trustXSendfileTypeHeader();
$response->headers->set('Content-type', 'image/jpg');
$response->sendHeaders();
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE, "name"); 

然后在我看来:

<img src="<?= $response ?>" />

但是找不到图片,我得到的图片网址是:

HTTP/1.0 200 OKCache-Control: publicContent-Disposition: inline; 文件名=

有什么想法吗?

【问题讨论】:

    标签: image symfony x-sendfile


    【解决方案1】:

    我遇到了类似的问题,我创建了一个返回图像的操作。

    public function getImgAction(){
        $f = fopen('/var/assets/example.jpg', "rb");
        $str = stream_get_contents($f);
        fclose($f);
        $response = new Response($str, 200);
        $response->headers->set('Content-Type', 'image/jpg');
        return $response;
    }
    

    【讨论】:

    • 谢谢,但是如何在视图中打印此图像?谢谢!
    【解决方案2】:

    确保为 apache 安装并启用了 mod_xsendfile

    第 3 行有一个错字,将变量传递给类构造函数时的字符串定界符(应该是 $response = new BinaryFileResponse($path);,但我假设你知道这一点)。

    【讨论】:

      猜你喜欢
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2012-02-03
      • 2011-04-04
      相关资源
      最近更新 更多