如何使用 Laravel 显示图像

我正在尝试将图片上传到我的网站,但它没有显示

http://img3.mukewang.com/6172658a0001ddc606830313.jpg

这是 HTML


<div class="col-md-10">

 <div class="post-image">

    <canvas style="display: none" id="canvas-preview" style="overflow:hidden;"></canvas>

    <img id="img-preview" src="{{ url('backend/'). ($posts->preview_image?'/'. $posts->preview_image:'/post_preview/post-image.png')}}"> </div>

    <div class="m-b-10">

        <a id="upload-submit" class="btn btn-warning btn-block btn-sm" onclick="file.click();"><i class="fa fa-upload"></i> Upload Picture </a>

         {{  Form::file('preview_image', ['class'=> 'btn btn-warning btn-block btn-sm','id'=>'file','style'=>'visibility:hidden']) }}

  </div>

  </div>

控制器:


public function create()

    {        $this->authorize('create', Post::class);


        return \View::make('posts.edit')

            ->with('posts', new \App\Models\Post);

    }

和模型:


class Post extends Model

{

    const DEFAULT_PREVIEW_IMAGE = "post_preview/post-image.png";


    /**

     * get preview image

     * @return mixed|string

     */

    public function getPreviewImage()

    {

        return $this->show_preview_image?$this->preview_image:static::DEFAULT_PREVIEW_IMAGE;

        //return $this->preview_image;

    }

}

我尝试添加多个扩展像jpg,jpeg或gif在模型中,但问题仍然存在


红颜莎娜
浏览 307回答 2
2回答

DIEA

对于 Laravel 图像应该存储在存储文件夹中,或者您可以在存储文件夹中创建一个文件夹。您还需要创建符号链接&nbsp;php artisan storage:link这是将文件上传到存储文件夹的方法if ($request->hasFile('file_name')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file = $request->file('file_name');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fileName = str_replace(' ', '-', $file->getClientOriginalName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $uploadPath = storage_path('app/public/folder_name');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $path = $request->file('file_name')->move($uploadPath, $fileName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $uploadPathFile = $fileName;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;然后将路径保存到数据库。用于在刀片中显示图像<img src="/storage/folder_name/{{$data->image}}" alt="No image available" class="img-thumbnail">这里 $data->image 只是路径
打开App,查看更多内容
随时随地看视频慕课网APP