如何在cakephp 3.2中下载excel文件后解除链接


How to unlink the excel file after download in cakephp 3.2

我正在为我的项目做一个excel插件,我想在用户下载完成后取消该excel文件的链接或在弹出窗口中显示下载。

我已经尝试了解链接代码来完成事情,但由于有响应,我有点困惑如何使它。下面我附上了部分代码。如有任何建议,我将不胜感激。

 $filename = time() . "-ocma-sales-report-" . date("Y-m-d") . ".xlsx"; //'.time() . '-ocma-sales-report-' . date("Y-m-d").'.xls'
                        $objWriter->save("temp_excel/$filename");
                        $filePath = 'temp_excel/' . $filename;
                        $this->response->file($filePath, ['download' => TRUE, 'name' => $filename]);
                        return $this->response;
                        //unlink($filename);
                        exit;

您可以使用FileSystem/File类来创建、修改和删除文件。此外,下载文件,你必须使用简单的php代码,因为$this->response->file($filePath, ['download' => TRUE, 'name' => $filename]);不允许在执行函数后进行任何操作。

ob_clean();
$filePath = 'temp_excel/' . $filename;
$size   = filesize($filePath);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename); 
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
readfile($filePath);
$file = new 'Cake'Filesystem'File($filePath);
$file->delete();
exit();