簡體   English   中英

Laravel 5.2從存儲目錄訪問文件

[英]Laravel 5.2 Access a file from storage directory

我已將上傳內容放入存儲文件夾中。 現在,我想在我的視圖中顯示這些上傳內容。 我試過這樣做:

視圖:

<img src="{{route("image_download", ['id' => Auth::guard('admin')->user()->AccountOfficerID, 'filename' => Session::get('avatar')] )}}" class=img-circle alt="Avatar" />

路線:

Route::get('images/{id}/{$filename}', ['as' => 'image_download', function ($id, $filename)
{
    $path = storage_path() . '/uploads/images/admins/' . $id . '/icon_size/' . $filename;

    if (File::exists($path)) {
        $type = File::mimeType($path);

        return response()->download($path);
    }
}]);

以及我搜索過的其他各種方法。 但沒有一個對我有用。 我檢查了路徑,這是正確的。 但是,它根本不顯示任何圖像。 我希望有人能指出我走上正軌。 謝謝。

編輯瀏覽器控制台中似乎有404錯誤與文件有關。 但我可以使用瀏覽器返回的路徑訪問該文件。

這只是參數中的一個錯誤。 我不小心放了一個$符號。 謝謝你的幫助。

首先你修改了你的config / filesystems.php嗎?

你也不需要在src圖像中調用route()。

`<img src="{{route("image_download", ['id' => Auth::guard('admin')->user()->AccountOfficerID, 'filename' => Session::get('avatar')] )}}" class=img-circle alt="Avatar" />`

而是執行asset()並將路徑放在<a>如果您想要鏈接的話

<img src="{{asset("image_source")}}" class=img-circle alt="Avatar" />

查看本教程,了解如何正確使用Laravel Storage

試試這個:

<img src="{{ url('image_download/'.$id.'/'.$filename) }}"/>

暫無
暫無

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

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