我不知道我在這里做錯了什么,但我一輩子都無法讓下載功能在 Laravel 5.6 中作業
我將檔案保存到公用檔案夾中,如下所示:
在網頁上,這些影像在 HTML<img>
標記中正確顯示,當我查看影像的詳細資訊時,它顯示它指向https://mydomain.test/storage/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png
.
但是,當我嘗試從服務器下載此檔案時,我不斷收到有關該檔案不存在的例外錯誤(例如:"data":"The file \"public\/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png\" does not exist")
。我嘗試了所有我能想到的路徑字串組合,但一切都會導致相同的錯誤。
我嘗試過使用storage_path
,我嘗試過使用原始路徑,我嘗試過硬編碼路徑
[2022-09-08 09:58:55] local.ERROR: Failed to download media file: The file "/home/vagrant/Code/dras/storage/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png" does not exist
[2022-09-08 09:59:38] local.ERROR: Failed to download media file: The file "public/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png" does not exist
[2022-09-08 10:00:14] local.ERROR: Failed to download media file: The file "https://mydomain.test/storage/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png" does not exist
[2022-09-08 10:17:18] local.ERROR: Failed to download media file: The file "app/public/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png" does not exist
[2022-09-08 10:45:50] local.ERROR: Failed to download media file: The file "storage/public/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png" does not exist
唯一不會引發“檔案不存在”例外的組合是當我使用路徑時
return response()->download( 'storage/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png' );
但是,該命令會引發完全不同的錯誤:
Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header() in file /home/vagrant/Code/dras/app/Http/Middleware/Cors.php on line 27
uj5u.com熱心網友回復:
路徑:“public/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png\ 暗示windows和linux系統之間的沖突。路徑中不能同時有/和\。它必須是或。不幸的是,我去過那里,沒有簡單的方法可以得到過去。您必須使用 str_replace 方法手動替換(/)或()與另一個。首先檢查您的作業系統視為有效檔案路徑的內容。它應該是:\public\LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH.png 或 /public/LIizYnRWEqYOgTPlzTxbmuSbTMNYIFrnjLP1YWVH .png
編輯:供將來參考,這是最終解決問題的方法,
Laravel 中的 BinaryFileResponse 未定義
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507609.html