【问题标题】:Symfony 2.7, Stream a file with StreamedResponse, requests for informationSymfony 2.7,使用 StreamedResponse 流式传输文件,请求信息
【发布时间】:2015-12-12 19:37:21
【问题描述】:

我正在开发一个带有视频的 symfony 应用程序。我需要流式传输这些视频,然后在 html5 播放器中显示。 我使用了这篇博文:http://ailoo.net/2013/03/stream-a-file-with-streamedresponse-in-symfony/,我认为它运行良好。

$response = new StreamedResponse(
        function () use ($file) {
                    readfile($file);
                }, 200, array('Content-Type' => 'video/' . $formatVideo)

            );
return $this->render('SQMovieBundle:Movie:displayMovie.html.twig', array('test' => $response));

但这是我第一次使用缓冲区之类的东西,我读到我有一些与 apache 相关的配置(我在 mac os 上使用 mamp)。 而且我不知道在树枝上显示我的视频。

请随时给我指示。

【问题讨论】:

  • 它是否将视频或视频的 URL 传递给 Twig?
  • 它传递了一个对象 StreamedReponse,{{ dump(test) }} 给了我:StreamedResponse {#1126 ▼ #callback: Closure {#1128 ▶} #streamed: false +headers: ResponseHeaderBag {#1157 ▶} #content: null #version: "1.0" #statusCode: 200 #statusText: "OK" #charset: null }
  • @malcolm 我知道如何使用 html 5 播放器,但我需要知道如何从这个对象响应或缓冲区 idk 访问我的文件以实现 src=""。跨度>
  • 不应该像src="path/to/controller/with/streamed/response"吗?

标签: php html apache symfony


【解决方案1】:

在 Symfony 中流式传输文件有更简洁的方式,只需返回 BinaryFileResponse 类的实例:

return new \Symfony\Component\HttpFoundation\BinaryFileResponse($pathToTheFile);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多