0

I want to show the image that uploaded to local disk storage. the file located at: storage/user/ directory.

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

and return it to ajax and append it in the blade page.

      return response()->json(["response"=>$image]);
HamHp
  • 23
  • 8

2 Answers2

0

You can do it like this. I would like to change the JSON response first. return response()->json(['data' => $image, 'status' => 'success', 'code' => 200]);

In Ajax, once your file uploaded

$.ajax({
.
.
.
success : function (res) {
       if (res.code == 200) {
          $('#image').text(res.data);
      }
});

Add one tag <p id="image"></p> in blade file.

can change your file path in the filesystems.php file under the config folder.

   'local' => [ 'driver' => 'local',  'root' => storage_path('__FOLDER_PATH__'), ],
   'public' => ['driver'  => 'local',
                'root' => storage_path('__FOLDER_PATH__'),
                'url' => env('APP_URL').'/storage', 
                'visibility' => 'public',],```
Jayant
  • 280
  • 2
  • 6
  • I think because the file located in local disk , not public disk. it is not accessible. I want to solve this issue. if I put the file in storage/public directory, it will be accessible publicly. But I don't want it be publicly accessible. – HamHp Jun 06 '21 at 19:19
  • 'default' => env('FILESYSTEM_DRIVER', 'local'), 'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path(''), ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ], – HamHp Jun 06 '21 at 19:24
  • you can change your storage path, make it configurable from `.env` and use it in `root` key in `filesystems.php`. – Jayant Jun 06 '21 at 19:31
0

I have found how to show image that Stored in local disk of Storage in laravel: Routes:

 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 is the path of image in local storage(local disk, not public disk)

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

return respose and append it in the blade by 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;

and It will show the image that Stored in local disk.

HamHp
  • 23
  • 8