首页 > 解决方案 > 如何在lravel中显示存储在本地磁盘存储中的图像

问题描述

我想显示上传到本地磁盘存储的图像。该文件位于:storage/user/ 目录。

 $url = Storage::url('users/file.jpg');
 $image="<img src='$url' />;

并将其返回给 ajax 并将其附加到刀片页面中。

      return response()->json(["response"=>$image]);

标签: laravel

解决方案


我找到了如何在 laravel 中显示存储在本地存储磁盘中的图像:路线:

 Route::post('/clientarea/form/{id}/responses/individual', [ResponsesController::class,'individual']);

Route::get('/image/show/users/{userId}/{formId}/{fileName}',[ResponsesController::class,'showImage']);

ResponsesController::class,'individual' :

$answer 是图像在本地存储中的路径(本地磁盘,不是公共磁盘)

$response.= "<img alt='image' title='image' class='answersImage' src='/image/show/$answer'>";
  return $response;

返回 respose 并通过 jquery 将其附加到刀片中:

   success: function(data, textStatus, jQxhr ){
                 
                    $('#respContainer').html(response);
                    
                };

ResponsesController::class,'showImage'] :

$path='users/'.$userId.'/'.$formId.'/'.$filename;
     $file = Storage::get($path);
            $response = response()->make($file, 200);
            $response->header('Content-Type', 'image/jpg');
            return $response;

它将显示存储在本地磁盘中的图像。


推荐阅读