Laravel + Vue $ request-> hasFile('image')返回null

时间:2018-11-12 09:24:08

标签: laravel vue.js

  • Laravel 5.7
  • upload_max_filesize = 200m
  • post_max_size = 250m

$request->hasFile('image')返回null,即使我可以清楚地看到包含图像密钥的请求返回的有效base64(已验证为图像)

控制器

if ($request->hasFile('image')) {
    return 'yes';
}else{
    echo 'no';
    return $request->all();
}

<form @submit.prevent="saveOoi" enctype="multipart/form-data">
    <div><img :src="image" class="embed-responsive"></div>
    <input type="file" v-on:change="onFileChange" class="form-control mb-3" name="image">
</form>

脚本

data() {
    return {
                image: '',
                errors: new Errors(),
            }
        },    
methods: {
        onFileChange(e) {
            let files = e.target.files || e.dataTransfer.files;
            if (!files.length)
                return;
                this.createImage(files[0]);
                this.$data.image = files[0];
         },
         createImage(file) {
             let reader = new FileReader();
             let vm = this;
             reader.onload = (e) => {
             vm.image = e.target.result;
             };
         reader.readAsDataURL(file);
         },
         saveOoi() {
             axios.post('ooi', this.$data)
             .then(response => console.log('sent', response))
             .catch(error => this.errors.record(error.response.data));
         }
    }

我具有chrome的vue扩展名,当我选择文件时,可以看到该文件已在data.image中设置。

我在这里想念什么?

2 个答案:

答案 0 :(得分:1)

hasFile不起作用,因为它不是文件。您已经将文件转换为base64并将其发布为json,因此您将以$ request-> input('image')的身份访问base64-然后,您必须执行以下操作:How to save a PNG image server-side, from a base64 data string

如果您要上传文件并使用hasFile在服务器端对其进行访问,则可以执行以下操作:

const fd = new FormData()
fd.append('image', this.file)
fd.append('type', this.name)  // any other fields
// fd.append... more fields
axios.post('ooi', fd)
<b-form-file
  :id="fileName"
  v-model="file"
/>

答案 1 :(得分:0)

在您的控制器中

public function store(Request $request)
{
    if($request->hasFile('image_file')){
        $file = $request->file('image_file');
        $name_image = time().$file->getClientOriginalName();
        $file->move(public_path().'/images/', $name_image);
    }
}

在您的Vue组件中

<template>
  <form method="post" enctype="multipart/form-data">
  <div class="image">
      <input type="file" id="image_file" :v-model="image_file" name="image_file" @change="onFileChange"
        accept="image/*" class="input-file">
         <p v-if="!url">Click to browse your image</p>
        <img v-if="url" :src="url" class="img img-responsive full-width" />
    </div>
  </form>
</template>
<style>

</style>
<script>
export default{
   data(){
        return{
           url: null
        }
   },

   methods: {
      onFileChange(e) {
         let files = e.target.files || e.dataTransfer.files;
         if (!files.length) {
             console.log('no files');
         }
         console.log(files);
         console.log(files[0]);
         const file = files[0];
         this.url = URL.createObjectURL(file);
         const formData = new FormData();
         formData.append('image_file', file, file.name);
         axios.post('http://127.0.0.1/....', formData, {}).then((response) => {
            console.log(response.data)
         })
         .catch(function(err){
             console.log(err)
         });
      }
   }
}
</script>