在Symfony中返回一个内存文件

Ast*_*oth 0 php symfony

我试图在Symfony 控制器中返回BinaryFileResponse响应,但它要求路径File对象.问题是我通过SOAP服务获取文件,它创建了一个像这样的对象:+ + + . filenamebase64 encoded contentmimeextra stuff

我真的不想将它保存到磁盘然后创建响应...

也许我现在被蒙蔽了,但有没有办法在不创建磁盘文件的情况下发送它?

这是行动:

public function downloadAction($hash){
   $document = $this->get('soap_services.document_service')->findDocument($hash);

   return $this->file($SymfonyFileObjectOrPath, $fileName);
}
Run Code Online (Sandbox Code Playgroud)

$document对象提供以下相关方法:getMime,getName,getContent.

这就是我想要用来做出响应而不创建File对象(以及它暗示的物理文件).

Con*_*tin 9

你好,也许你应该考虑只是给出一个响应并强制下载"手动".

// Generate response
$response = new Response();
$filename = 'yourFileName.txt';

// Set headers
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', $yourMimeType );
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '";');
$response->headers->set('Content-length',  strlen($yourContentString));

// Send headers before outputting anything
$response->sendHeaders();

$response->setContent( $yourContentString );

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

也许这样的事情可以解决问题.你能尝试一下吗,也许可以稍微改一下?