我正在製作一個使用Symfony 2的「下載」控制器,它的唯一目的是發送標題,以便我可以強制.csv文件下載,但它不能正常工作。如何使用Response對象強制下載Symfony 2中的.csv文件?
$response = new Response();
$response->headers->set('Content-Type', "text/csv");
$response->headers->set('Content-Disposition', 'attachment; filename="'.$fileName.'"');
$response->headers->set('Pragma', "no-cache");
$response->headers->set('Expires', "0");
$response->headers->set('Content-Transfer-Encoding', "binary");
$response->headers->set('Content-Length', filesize($fileName));
$response->prepare();
$response->sendHeaders();
$response->setContent(readfile($fileName));
$response->sendContent();
$fileName
是"info.csv"
字符串。這是我在我的控制器中的行爲,沒有返回聲明。當我嘗試返回Response
對象時,文件的內容顯示在瀏覽器中,而不是我的預期結果。
我發現的問題是,在一些網頁我得到我的info.csv file
,但在anothers我得到的是一個信息:
No webpage was found for the web address: http://mywebpage.com/download
Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.
我完全確保該文件存在,所以肯定會有另一件事情是錯誤的。此外,routing.yml工作正常,因爲我確實從其他頁面鏈接到該路徑的文件。 Apache錯誤日誌沒有顯示任何關於它的信息。
有沒有人在Symfony 2上強制下載.csv文件?如果是這樣,我做錯了什麼?
$ tpldata未定義 – 2015-07-01 20:10:37