【问题标题】:Unable to display image in vue component form storage of Laravel无法在 Laravel 的 vue 组件形式存储中显示图像
【发布时间】:2020-08-07 13:53:57
【问题描述】:

我无法在 Laravel 的 Storage 目录中的 vue 组件中显示图像。请帮我解决这个问题:

图像在数据库中存储为public/avatar/TSD50oeYXnkrX1nQ38N9zljP1FRdP42yPhJOrEKg.png

如果我删除公共路径并执行此操作<img :src="'../storage/avatar/31deG4gnbO3EwO2s26lnUe0KzJjskFfm8lPIGFeM.jpeg'" width="100">

我可以从 storage/public/avatar 文件夹中看到 vue 组件中显示的图像

获取单个用户详细信息的方法

public function user($id){
        return $user = User::find($id);
    }

图片保存方法

public function profilePic(Request $request){
        $this->validate($request,[
            'image'=>'required|mimes:jpeg,jpg,png'
        ]);
        $image = $request->image->store('public/avatar');
        $authUser = auth()->user()->id;
        $user = User::where('id',$authUser)->update([
            'profilePic'=> $image
        ]);
        return redirect()->back();
    }

vue:

<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Profile Component</div>

            {{user.profilePic}}
//how can i display image here


<div class="card-body">
                        <form @submit.prevent="updateProfilePic" method="post" enctype="multipart/form-data">
                            <input type="file" name="image" class="form-control"v-on:change="onImageChange">
                            <button type="submit" class="btn btn-primary">Submit</button>

                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        props:['userid'],
        data(){
            return{
                image:'',
                user:[]

            }
        },
        mounted() {
            this.getUser()
            console.log('Component mounted.')
        },
        methods:{
            onImageChange(e){
                console.log(e)
                this.image = e.target.files[0];

                },
            updateProfilePic(){
                const config={
                    headers:{
                        "content-type":"multipart/form-data"
                    }
                }
                let formData = new FormData();
                formData.append('image',this.image);

                axios.post('/profile-pic',formData,config).then((response)=>{
                    this.image='';
                    this.getUser()



                }).catch((error)=>{
                    console.log(error)
                    this.allerrors = error.response.data.errors
                })
            },
            getUser(){
                axios.get('/user/'+this.userid).then((response)=>{
                    this.user = response.data
                }).catch((error)=>{
                    alert('some error')
                })
            },
        }
    }
</script>

【问题讨论】:

    标签: laravel vue.js laravel-7


    【解决方案1】:

    要使这些文件可以从 Web 访问,您应该创建一个从 public/storagestorage/app/public 的符号链接。 要创建符号链接,请运行:

    php artisan storage:link
    

    然后在 vue 组件中:

    <img :src="'/storage/images'+filename" width="100">
    

    但如果你有一个类别数组,每个类别都有一张图片

     <img v-for="category in categories" :key="category.id" :src="'/storage/' + category.image" alt=""  />
    

    category.image 等于 images/ScjDpeUs12vvJHouHTWXQ12BBghzBoImDiumuNPx.jpg

    【讨论】:

      【解决方案2】:

      你可以这样显示图片。Here is a related question

      <img :src="'/avatar/' + user.profilePic" alt="Profile Photo">
      

      【讨论】:

      • 这对我不起作用图片路径是 public/avatar/filename.jpg
      • 这会显示 inage 吗?
      【解决方案3】:

      很可能您没有在存储和公用文件夹之间设置符号链接。 尝试运行此命令:php artisan storage:link

      你可以在这里了解更多:https://laravel.com/docs/7.x/filesystem#the-public-disk

      【讨论】:

      • 您的图片直接位于哪个文件夹中?您的默认存储驱动程序是什么?
      • 嗨图片在 storage/public/avatar/filename.png
      猜你喜欢
      • 1970-01-01
      • 2021-02-07
      • 2020-12-28
      • 1970-01-01
      • 2020-10-11
      • 2021-12-16
      • 1970-01-01
      • 2020-12-10
      • 2017-09-03
      相关资源
      最近更新 更多