在我用Laravel 8創建的web應用程式中,我需要下載一個.xml檔案,檔案名必須是動態創建的,如標題所示。 這是因為檔案名是按照一個特定的規則組成的,必須包括今天的日期。
$filename = 'FirstPart_'.date("Y-m-d").'_000.xml'/code>
到目前為止,為了測驗回應和下載,我給檔案起了一個固定的名字,如下所示,并且下載被正確執行。
- 帶有固定檔案名的代碼
- 帶有固定檔案名的代碼
$response = Response::create($xml, 200)。)
$response->header('Content-Type'/span>, 'text/xml'/span>)。
$response->header('Cache-Control'/span>, 'public'/span>);
$response->header('Content-Description', 'File Transfer') 。
$response->header('Content-Disposition', 'attachment; filename="TestXMLDownload.xml"')。
$response->header('Content-Transfer-Encoding', 'binary');
return $response;
但是當我試圖用我的自定義檔案名下載檔案時,下載失敗。
- 帶有自定義檔案名的代碼
- 帶有自定義檔案名的代碼 。
$response = Response::create($xml, 200) 。
$response->header('Content-Type'/span>, 'text/xml'/span>)。
$response->header('Cache-Control'/span>, 'public')。
$response->header('Content-Description', 'File Transfer') 。
$response->header('Content-Disposition', 'attachment; filename="".$filename."');
$response->header('Content-Transfer-Encoding', 'binary');
return $response;
我感謝任何建議或意見
uj5u.com熱心網友回復:
你可以試試這段代碼。PHP通過特定的單引號接受動態檔案名。我已經在這里改變了引號的格式。
$response->header('Content-Disposition', 'attachment; filename=" ' /span> . $filename . '"')。)
uj5u.com熱心網友回復:
你的代碼在這一行有一個錯誤,在這種情況下,"$filename "附加在你的頭中,而不是動態檔案名值。
$response-> header('Content-Disposition', 'attachment; filename="。 $filename.""')。)
嘗試這個解決方案:
$filename = "TestXMLDownload.xml"/span>。
$response = Response::create($xml, 200)。)
$response->header('Content-Type'/span>, 'text/xml'/span>)。
$response->header('Cache-Control'/span>, 'public'/span>);
$response->header('Content-Description', 'File Transfer') 。
//改變你代碼中的那一行。
$response->header('Content-Disposition', 'attachment; filename=' . '" ' . $filename . '"')。)
$response->header('Content-Transfer-Encoding', 'binary')。
return $response;
在你的檔案名中添加$filename,以傳遞給$response->頭。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/333697.html
標籤:
上一篇:如何使用其他類中的函式
下一篇:如何用法語顯示十位數的小數部分
