0

I can upload file in database and it is stored in my upload files.

Now I want to display it in in my show.blade.php, so I did this, but it is not working.

<iframe src="/storage/uploads/{{ $file->file_path }}"  width="400" height="400"></iframe>

as result I got this not found in show.blade.php

So how can I display it? This is my FileController.php

class FileUpload extends Controller
{
  public function createForm(){
    return view('file-upload');
  }

  public function fileUpload(Request $req){
        $req->validate([
        'file' => 'required'
        ]);

        $fileModel = new File;

        if($req->file()) {
            $fileName = time().'_'.$req->file->getClientOriginalName();
            $filePath = $req->file('file')->storeAs('uploads', $fileName, 'public');

            $fileModel->name = time().'_'.$req->file->getClientOriginalName();
            $fileModel->file_path = '/storage/' . $filePath;
            $fileModel->save();

            return back()
            ->with('success','File has been uploaded.')
            ->with('file', $fileName);
        }
   }

   public function show(File $file)
    {
        // $news=News::find($id);
        return view('show',compact('file'));
    }

}

this is web.php

Route::get('/upload-file', [FileUpload::class, 'createForm']);

Route::post('/upload-file', [FileUpload::class, 'fileUpload'])->name('fileUpload');
Route::get('/uploadshow', [FileUpload::class, 'show']);

Thanks

Screen short of ifream enter image description here

Shahed Khan
  • 67
  • 11
  • What have you tried to resolve the problem? Is `storage` a folder, or even a symlink, in your webroot? – Nico Haase Dec 03 '21 at 06:52
  • Does https://stackoverflow.com/questions/30191330/laravel-5-how-to-access-image-uploaded-in-storage-within-view help? – Nico Haase Dec 03 '21 at 06:52
  • Or this? https://stackoverflow.com/questions/55479687/get-read-laravel-5-8-storage-non-public-folder-files-to-view – Nico Haase Dec 03 '21 at 06:53
  • Judging from the screenshot: did you try to remove the `=` at the beginning of the `src` attribute? That looks pretty wrong to me – Nico Haase Dec 03 '21 at 09:19

3 Answers3

0

You need to put proper path in iframe using asset()

<iframe src="{{ asset('storage/app/public/uploads/') }}/{{ $file->file_path }}"  width="400" height="400"></iframe>
Leena Patel
  • 2,423
  • 1
  • 14
  • 28
0

You may use the URL method to get the URL for a given file. If you are using the local driver, this will typically just prepend /storage to the given path and return a relative URL to the file. If you are using the s3 driver, the fully qualified remote URL will be returned:

 <iframe src="{{ URL::asset('storage/uploads/'.$file->file_path}}"width="400" height="400"></iframe>
-1

Your route uploadshow should have parameter fileId like this uploadshow/{fileId}.

Then in your show function will be show($fileID). $file = File:find($fileID)

In your frame src, asset($file->file_path)

MuAf
  • 29
  • 1
  • 7
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 03 '22 at 19:46