【发布时间】:2016-07-14 06:05:40
【问题描述】:
我想通过 Slim Framework 下载文件(我使用 Slim Framework 的原因是因为我想编写一个简单的 REST API)。我找到了这篇文章: Download file from Slim Framework 2.4 和这篇文章:http://help.slimframework.com/discussions/questions/359-file-download。我按照方法。这是我的代码:
$app->get('/download/:id', 'authenticate', function($id) use ($app)
{
$log = '../download/'.$id.'/myfile.zip';
$res = $app->response()->headers();
$res['Content-Description'] = 'File Transfer';
$res['Content-Type'] = 'application/octet-stream';
$res['Content-Disposition'] ='attachment; filename=' . basename($log);
$res['Content-Transfer-Encoding'] = 'binary';
$res['Expires'] = '0';
$res['Cache-Control'] = 'must-revalidate';
$res['Pragma'] = 'public';
$res['Content-Length'] = filesize($log);
readfile($log);
// NOTE:
// $response = array();
// $response["error"] = false;
// echoRespnse(200, $response);
});
我正在使用 Google Chrome 的 Advanced Rest Client 应用程序进行测试。问题是下载文件后浏览器挂起。如果我在源代码中删除了 NOTE 部分,浏览器将不会再次挂起。但我收到“意外的令牌 P”错误。
有人可以帮忙吗?谢谢。
【问题讨论】: