【问题标题】:Vue js and laravel keep an uploaded image permanentlyVue js 和 laravel 永久保留上传的图片
【发布时间】:2020-11-15 23:14:48
【问题描述】:

我写了一个代码来上传一个图像文件并且已经可以工作了,我也可以将图像 url 保存在我的数据库中。问题是图像没有永久加载。这意味着我上传图片后可以看到图片,但在我保存图片的那一刻,网址不再有效。有谁知道如何修理它?我的意思是将图像永久存储在我的项目中。我希望用户上传和使用他们自己的图像?谢谢你的帮助。

在模板中上传 <input style="margin-left: 35px" type="file" @change="onFileChange" />

显示图像 <img v-if="data.image" :src="data.image" style="max-width:450px;vertical-align:middle;margin:0px 5px 5px"/>

脚本方法

      onFileChange()e {
      const file = e.target.files[0];
      this.data.image = URL.createObjectURL(file);
    },```




【问题讨论】:

    标签: laravel image vue.js file-upload


    【解决方案1】:

    你可以在你的控制器中做这样的事情:

        $request->validate([
            'image' => 'required|image|mimes:jpeg,jpg,png,gif|max:2048',
        ]);
    
        $imageName = time().'.'.$request->image->extension();  //What you store in DB
    
        $request->image->move(public_path('images'), $imageName); //Put the image in a public folder 
    

    您可以从那里通过 URL 访问它。

    【讨论】:

      猜你喜欢
      • 2020-07-13
      • 2020-11-14
      • 2020-09-27
      • 1970-01-01
      • 2022-10-07
      • 2021-07-19
      • 2018-05-28
      • 2020-09-30
      • 1970-01-01
      相关资源
      最近更新 更多