Kohana 3.2.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我在操作中添加
$this->auto_render = FALSE;
并在最后删除exit;
后,它就可以工作了。It works after I added
$this->auto_render = FALSE;
in the action and removed theexit;
at the end.您应该使用 send_file()
调用此方法后,不能进行任何处理完成,发送文件时方法调用
exit
。You should use send_file()
After the call of this method no any processing can be done, method calls
exit
when file was sent.这个问题,所以可能值得尝试那里使用的确切代码,看看它是否有效。
唯一的区别似乎是删除了
->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 :)@阿特玛:
发送文件完全正确,但是:
这并不完全正确。当然,您可以在调用退出后进行任何处理。
有几种方法。首先可以是一个回调: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 :)