我正在为我的应用程序使用 CakePHP 3.4+。
本地路径中有一个 XML 文件,点击链接时需要下载。在根据我的应用程序检查一些要求后,我想从控制器的操作中返回下载
public function downloadXml()
{
if ($this->_checkMembership()) {
try {
// file path /webroot/agency/data.xml
$xmlLink = WWW_ROOT . 'agency/data.xml';
$this->response->withFile($xmlLink, [
'download' => true,
'name' => 'data.xml',
]);
return $this->response;
} catch (NotFoundException $e) {
$this->Flash->error('Requested file not found. Try again');
return $this->redirect(['action' => 'index']);
}
}
}
在模板中
<?= $this->Html->link(
__('Download the site'),
[
'action' => 'downloadXml'
],
) ?>
但这仅在单击链接时显示空白页面