提问者:小点点

无法在Laravel的vue组件窗体存储中显示图像


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

图像以公共/avatar/TSD50oeYXnkrX1nQ38N9zljP1FRdP42yPhJOrEKg的形式存储在数据库中。巴布亚新几内亚

如果我删除公共路径并执行此操作

我可以在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>

共3个答案

匿名用户

要从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=""  />

类别。图像equalimages/scjdpeus12vvvjhouhtwxq12bbghzboimdiumunpx。jpg

匿名用户

您可以这样显示图像。这是一个相关的问题

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

匿名用户

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

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