开发者

Kohana 3.2.0 cannot output an image correctly

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_开发者_如何学CPortal 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;
    }
}


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


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.


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 :)


@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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜