Symfony BinaryFileResponse 设置文件名
Symfony BinaryFileResponse set filename
当 Symfony2 控制器使用 BinaryFileResponse 响应返回文件时,是否可以设置自定义文件名?
是的。 BinaryFileResponse
class 有一个方法 setContentDisposition()
将文件名作为第二个参数。
第一个参数是文件的传送方式。它可以是ResponseHeaderBag::DISPOSITION_ATTACHMENT
(或者只是字符串"attachment"
)如果文件应该被提供下载,或者ResponseHeaderBag::DISPOSITION_INLINE
(或者"inline"
)如果你想要文件被显示在浏览器中(例如,您可能想对图像执行此操作)。
完整代码示例:
<?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'
);
当 Symfony2 控制器使用 BinaryFileResponse 响应返回文件时,是否可以设置自定义文件名?
是的。 BinaryFileResponse
class 有一个方法 setContentDisposition()
将文件名作为第二个参数。
第一个参数是文件的传送方式。它可以是ResponseHeaderBag::DISPOSITION_ATTACHMENT
(或者只是字符串"attachment"
)如果文件应该被提供下载,或者ResponseHeaderBag::DISPOSITION_INLINE
(或者"inline"
)如果你想要文件被显示在浏览器中(例如,您可能想对图像执行此操作)。
完整代码示例:
<?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'
);