【问题标题】:How to unlink the excel file after download in cakephp 3.2如何在cakephp 3.2中下载后取消链接excel文件
【发布时间】:2016-09-16 10:48:07
【问题描述】:

我正在为我的项目做一个 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;

【问题讨论】:

  • 我无法告诉您如何在 CakePHP 中执行此操作,但 PHP 提供了多种工具来生成临时文件并自动删除它们,从 tmpfile() 函数到 php://temp 流包装器。如果您自己处理临时文件,则必须安排清理过程,因为删除迟早会失败。
  • 好的@ÁlvaroGonzález 我明白了。谢谢

标签: php excel cakephp cakephp-3.x cakephp-3.2


【解决方案1】:

您可以使用 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();

【讨论】:

  • 我能知道你是哪里人吗?
  • 印度、奥里萨邦、布巴内斯瓦尔
  • hii,它不工作。下面是我的代码 $filePath = 'temp_excel/' 。 $文件名; $this->response->file($filePath, ['download' => TRUE, 'name' => $filename]); $file = new \Cake\Filesystem\File($filePath); $文件->删除();返回 $this->response;退出;
  • 我已经修改了答案
  • 好的,我正在检查。
猜你喜欢
  • 1970-01-01
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多