如何在 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 class 来创建、修改和删除文件。此外,对于下载文件,您必须使用简单的 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();
我正在为我的项目做一个 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 class 来创建、修改和删除文件。此外,对于下载文件,您必须使用简单的 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();