Symfony BinaryFileResponse set filename

痞子三分冷 提交于 2019-12-09 14:42:42

问题


is it possible to set own filename when file is returned by Symfony2 controller throught BinaryFileResponse response?


回答1:


Yes. The BinaryFileResponse class has a method setContentDisposition() that takes the file name as the second argument.

The first argument is the way the file should be delivered. It can be ResponseHeaderBag::DISPOSITION_ATTACHMENT (or just the string "attachment") if the file should be offered for downloading, or ResponseHeaderBag::DISPOSITION_INLINE (or "inline") if you want the file to be shown in the browser (you may want to do this with images, for example).

A full code example:

<?php
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

$response = new BinaryFileResponse('/path/to/myfile');
$response->setContentDisposition(
    ResponseHeaderBag::DISPOSITION_ATTACHMENT,
    'file_name.txt'
);


来源:https://stackoverflow.com/questions/28919811/symfony-binaryfileresponse-set-filename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!