2011-09-11 34 views
1

我已閱讀線程output image in a Kohana 3.2 view,但代碼無法在我的電腦上運行。Kohana 3.2.0無法正確輸出圖像

我寫輸出到瀏覽器 圖像的動作(如果我改變了超類控制器,它仍然無法正常工作。),代碼如下:

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; 
    } 
} 

回答

0

似乎有在this SO question中是一個可行的,可接受的答案,因此可能值得嘗試使用那裏的確切代碼並查看它是否有效。

唯一的區別似乎要刪除的->send_header()片段,但你永遠不知道:)

+0

你好,我試圖刪除 - > send_headers(),該動作仍然不起作用。 –

+0

夠公平的。我能夠建議的唯一其他事情就是快速回顯'File :: mime($ filename)'以確保它確實獲得正確的文件。除此之外,對於更熟悉Ko3.2的任何人 - 我使用3.0和2.3 – Joe

1

它的工作原理我在行動加入$this->auto_render = FALSE;,並在年底取出exit;後。

+0

我已將您的未註冊帳戶合併到此帳戶中,現在可以接受此答案作爲正確答案。 –

1

您應該使用send_file()

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

這種方法可以做到沒有任何處理的呼叫後,當文件被送到方法調用exit

0

@atma: 完全正確的發送文件,但: 這不完全正確。在退出被調用後,你當然可以做任何處理。 有幾種方法。首先可以給一個回調:register_shutdown_function()另一種方法是創建一個「後處理類」, 其中有「函數__destruct()」,在調用退出後被調用。

只是我的2美分:)