Friday, March 31, 2017

Laravel preview and download of uploaded file/image

I would like to develop a page in which the user can associate (upload) a file (img, pdf, csv ...) to a database record and then having a preview of the file (image or icon) and download it with a click. I think it is a very common scenario but despite of the good laravel documentation and although I found some similar questions (this, this and this) I can't figure it out.

I successfully upload the file (and store the name in the database) with: - the blade view

<form action=""
      method="post" enctype="multipart/form-data">
  
  <input type="file" name="order_file" 
  <input type="submit" title="Upload"> Upload
</form>

  • and the controller

    public function uploadfile (Request $request, Order $order) {
    
      $this->validate($request, ['order_file' => 'image|mimes:jpeg,bmp,png|max:2000']);
      $filename = $request->file('order_file')->storeAs('orders',$request->file('order_file')->getClientOriginalName());
      $order->order_file = $filename;
      $order->save();
      return redirect()->route('orders.edit',['id'=>$order->id])->with(['flash_message' => 'Order file successfully uploaded (delay).']);
    }
    
    

In the view I add this part to show different URLS

<ul>
    <li> Order file :  </li>
    <li> URL order file :  </li>
    <li> Asset :  </li>
    <li> Storage URL :  </li>
    <li> <img src="????"> </li>
    <li> <a href="????">Download</a> </li>
  </ul>

The result is

but I do not understand the right way to complete



via AlexMI

Advertisement