从 symfony 中的控制器操作返回图像

Ayo*_*oub 4 symfony

我需要通过在 url 路径中提供其名称来访问图像,我尝试使用此代码但图像未显示

  /**
 *
 * @Route("images/{imgname}",name="workflow_image")
 */
public function WorkflowImageAction(Request $request,$imgname){



    $filepath = $this->get('kernel')->getRootDir().'/../web/images/workflow/'.$imgname;

    $file =    readfile($filepath);

    $headers = array(
        'Content-Type'     => 'image/png',
        'Content-Disposition' => 'inline; filename="'.$file.'"');
    return $file;
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*teo 6

如果您正在提供静态文件,则可以使用BinaryFileResponse

use Symfony\Component\HttpFoundation\BinaryFileResponse;

$file = 'path/to/file.txt';
$response = new BinaryFileResponse($file);

return $response;
Run Code Online (Sandbox Code Playgroud)

文档中有关在 Symfony2 中提供文件的更多信息。

希望这有帮助