簡體   English   中英

symfony2重定向到流文件

[英]symfony2 redirect to stream file

嗨,大家好,我在symfony2中有一個小問題。

您能告訴我如何在控制器中重定向以打開(流式傳輸)資產文件夾中的文件而不強制下載

例如:我在root / web / bundles / ace / file.pdf中有一個文件

我使用了這段代碼,但是無法打開/ stream文件。 此代碼強制瀏覽器下載

        $path = $this->get('kernel')->getRootDir().DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."web".DIRECTORY_SEPARATOR."bundles".DIRECTORY_SEPARATOR.
            "bsibkpm".DIRECTORY_SEPARATOR."download".DIRECTORY_SEPARATOR . $fileName;
    //echo $path;die();
    $content = file_get_contents($path);

    $response = new Response();

    $response->headers->set('Content-Type', 'application/pdf');
    $response->headers->set('Content-Disposition', 'attachment;filename="'.$fileName);
    $response->sendHeaders();
    $response->setContent($content);
    return $response;``

非常感謝您的幫助

葉誠萬

您應該將Content-Disposition設置為inline

$response->headers->set('Content-Disposition', 'inline;filename="'.$fileName);

PS檢查此捆綁包https://github.com/igorw/IgorwFileServeBundle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM