我有一個從陣列構建的選單,我正在將其中一個選單項直接鏈接到本地??存盤的下載。選單項正在這里構建:
['name' => 'Download Now', 'route' => 'file.download.test'],
這是我要走的路線:
Route::group(['prefix' => 'download'], function () {
Route::get('test', 'file\downloadController@testingDownload')
->name('file.download.test');
});
不過,我對控制器功能感到困惑。需要下載的檔案位于本地磁盤中名為“test_downloads”的檔案夾中,因此 /storage/test_downloads 是路徑。我在那個檔案夾中只有一個檔案,但我需要通過路由發送明確的檔案名嗎?我只想單擊選單項來觸發檔案的下載,稱為“Test_file.xslx”
public function testingDownload(){
$file = Storage::disk('local')->get($file_name);
$filepath = storage_path("test_downloads/{$data->file}");
return \Response::download($filepath);
}
uj5u.com熱心網友回復:
testingDownload()看來您正在使用變數$file_name和$data; 它們將是未定義的,因為它們尚未設定。
如果您不需要檔案名是“可變的”,則不需要通過路徑傳遞檔案名,因為您使用的是靜態檔案名,也許這更適合?
public function testingDownload(){
// Set the static file name you want to download
$file_name = 'Test_file.xslx';
// Using the given file_name, get the file from our configured storage path
$file = storage_path("test_downloads/{$file_name}");
// Send a download response with the file
return \Response::download($file);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/530040.html
