0

我的网站上有以下链接 -http://mywebsite/multimedia/pronounciation/265.mp3 它让我绕过控制器获得文件。但我想记录请求,然后返回这个文件。所以我创建了记录请求然后重新路由到文件的控制器:

class Controller_GetSound extends Controller {

    public function action_index() {
        Request::factory('multimedia/pronounciation/265.mp3')
                ->method(Request::POST)
                ->post($this->request->post())
                ->execute();
    }
}

但它没有按预期工作。如何从控制器返回资源文件?

4

2 回答 2

2

Kohana 有一个 send_file 函数。该功能在拆分大文件和发送正确的 mime 类型方面做得很好。

@see http://kohanaframework.org/3.3/guide-api/Response#send_file

您的代码应该是:

class Controller_GetSound extends Controller {

    public function action_index() {

        $this->response->send_file('multimedia/pronounciation/265.mp3',TRUE,array(
            'mime_type' => 'audio/mpeg',
        ))
    }
}

您实际上并不需要设置 mime_type。Kohana 将为您找到正确的 mime_type。

于 2013-12-20T22:16:19.137 回答
1

听起来您想实现称为X-Sendfile 的东西。我认为?

控制器看起来像这样:

class Controller_GetSound extends Controller {

    public function action_index() {
        $this->response->headers(array(
            'Content-Type' => 'audio/mpeg'
            'X-Sendfile' => 'multimedia/pronounciation/265.mp3',
        );
    }
}
于 2013-12-12T21:34:33.067 回答