簡體   English   中英

在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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM