symfony - 下载文件 [英] symfony - download files

查看:134
本文介绍了symfony - 下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在控制器中下载文件,但在控制台中,我只看到一堆奇怪的字符,而不是下载框。我正在关注此主题 Symfony2 - 强制文件下载



不知道发生了什么......试图找到最简单的解决方案。
这里是我的代码:

$ $ $ $ $ $ $ $ $ $ $ response $ new响应(file_get_contents($ file-> realPath),200 ,数组(
'Content-Type'=> $ file-> mimeType,
'Content-Length'=> filesize($ file-> realPath),
'Content -disposition'=>'attachment; filename ='。$ file-> name,
));
$ response-> send();

我甚至试着用header()和readfile()来使用最基本的例子。
我的服务器是否需要特殊配置?干杯。

解决方案

可以使用Symfony的内置 BinaryFileResponse

使用Symfony \ Component \HttpFoundation\BinaryFileResponse;

$ response = new BinaryFileResponse($ file);

请参阅关于提供文件的文档


I am trying download files in controller but in the console I see only really bunch of weird characters instead of download box. I am following this topic Symfony2 - Force file download.

Don't know what is going on... trying to find simplest solution. Here is my code:

 $response = new Response(file_get_contents($file->realPath), 200, array(
            'Content-Type' => $file->mimeType,
            'Content-Length' => filesize($file->realPath),
            'Content-Disposition' => 'attachment; filename=' . $file->name,
        ));
 $response->send();

I've even tried to use the most basic example with header() and readfile(). Does my server need special config or something? Cheers.

解决方案

Instead of rebuilding that kind of response, you could use Symfony's built-inBinaryFileResponse.

use Symfony\Component\HttpFoundation\BinaryFileResponse;

$response = new BinaryFileResponse($file);

Please take also a look in the documentation about serving files.

这篇关于symfony - 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆