Kohana 3.2.0 无法正确输出图像

发布于 2024-12-03 17:17:42 字数 621 浏览 0 评论 0原文

我已阅读线程 output image in a Kohana 3.2 view ,但该代码在我的计算机上不起作用。

我编写了一个将图像输出到浏览器的操作 (如果我将超类更改为Controller,它仍然不起作用。),代码如下:

class Controller_Portal extends Controller_Template {
    public function action_view() {
        $filename = "E:\workspace\myphoto.jpg";

        $this->response->headers('Content-Type', File::mime($filename))
            ->send_headers()  // If I remove this line, It still doesn't work
            ->body(file_get_contents($filename));

        exit;
    }
}

I have read the thread output image in a Kohana 3.2 view, but the code doesn't work on my computer.

I wrote an action that outputs an image to the browser
(If I changed the super class to Controller, it still doesn't work.), the code is like:

class Controller_Portal extends Controller_Template {
    public function action_view() {
        $filename = "E:\workspace\myphoto.jpg";

        $this->response->headers('Content-Type', File::mime($filename))
            ->send_headers()  // If I remove this line, It still doesn't work
            ->body(file_get_contents($filename));

        exit;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

[旋木] 2024-12-10 17:17:42

在我在操作中添加 $this->auto_render = FALSE; 并在最后删除 exit; 后,它就可以工作了。

It works after I added $this->auto_render = FALSE; in the action and removed the exit; at the end.

暗喜 2024-12-10 17:17:42

您应该使用 send_file()

$this->response->send_file($filename, NULL, array('inline' => true));

调用此方法后,不能进行任何处理完成,发送文件时方法调用exit

You should use send_file()

$this->response->send_file($filename, NULL, array('inline' => true));

After the call of this method no any processing can be done, method calls exit when file was sent.

土豪我们做朋友吧 2024-12-10 17:17:42

这个问题,所以可能值得尝试那里使用的确切代码,看看它是否有效。

唯一的区别似乎是删除了 ->send_header() 片段,但你永远不知道:)

There seems to be a working, accepted answer in this SO question, so it's probably worth trying the exact code used there and see if it works.

The only difference seems to be removed the ->send_header() fragment, but you never know :)

记忆で 2024-12-10 17:17:42

@阿特玛:
发送文件完全正确,但是:
这并不完全正确。当然,您可以在调用退出后进行任何处理。
有几种方法。首先可以是一个回调:register_shutdown_function() 另一种方法是创建一个“后处理类”,
其中有“function __destruct()”,该函数在调用退出后被调用。

只是我的 2 美分:)

@atma:
Totally correct with send file but:
This is not fully true. You can of course do any processing after the exit is called.
There are several methods. First of all could be an callback given to: register_shutdown_function() the other method would be to create a "post processing class",
which has "function __destruct()" which gets called after the exit is called.

Just my 2 cents :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文