如何通过<a> element?

时间:2016-12-21 18:55:27

标签: symfony twig phpexcel

I wrote some code to generate an excel (.xlsx) file for users to download on my website. I'm using Symfony2, although I don't think it is the reason I'm having this problem. Here is a snippet of my code:

$filename = "cmm_payment_report.xlsx";

header('Content-Type: application/vnd.ms-excel');
header(sprintf('Content-Disposition: attachment; filename="%s"', $filename));
header("Cache-Control: max-age=0");

$objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save("php://output");

If I open a new page and paste the link for this download into the browser, it works as normal and I'm able to download the file. However, if I put the link into a Twig template like so:

<a href="{{ path('report_payment_excel', {payPeriod: app.request.get('pay-period')}) }}" target="_blank">
    <button>Download as XLS</button>
</a>

it doesn't work. I click on the link and the browser indicates that it's loading, however nothing happens after it stops loading. If I right-click > open link in new tab, then it works. Adding target="_blank" doesn't fix the problem.

1 个答案:

答案 0 :(得分:1)

问题是我的var obj = {}; // variant one using dot-notation obj.prop = "whatever"; // identical result, different syntax: obj["prop"] = "whatever"; 元素包含在<button>Download as XLS</button>元素中,因此无意中提交了表单而不是链接。