简体   繁体   English

上传文件返回 NULL - VueJs3 与 Laravel 8

[英]Uploading Files return NULL - VueJs3 with Laravel 8

I have a problem I tried many things but is doesn't fixed so please I need your help我有一个问题我尝试了很多东西但没有解决所以请我需要你的帮助

this is my Vue Temp:这是我的 Vue Temp:

<form @submit.prevent="createCat" enctype="multipart/form-data" method="POST">

   <div class="mt-1 relative rounded-md shadow-sm mb-3">
      <input id="cat_name" type="text" v-model="categories.cat_name" />
   </div>

   <div class="mt-1 relative rounded-md shadow-sm mb-3 w-50">
      <input name="img_url" id="img_url" accept="image/*" type="file">
   </div>                       
   <button type="submit">Add</button>

</form>
data(){
    return{
      categories:{
         cat_name: '',
         img_url: null,
      },
    }
},
methods:{
   createCat(){
      let data = new FormData();
      data.append('cat_name', this.categories.cat_name);
      data.append('url', this.categories.img_url);
      this.$inertia.post('/categories', this.categories)
           .then(()=>{
               //     
           })
   },
}

and in the Controller I tried debug the requests but it always return NULLController我尝试调试请求,但它总是返回NULL

public function store(Request $request)
    {
        dump($request->file('img_url'));
        dump($request->cat_name);
        dump($request->all());
    }

The Result结果

null

"name"

array:2 [▼

  "cat_name" => "name"

  "img_url" => null
]

As per official documentation根据官方文档

<template>
  <form @submit.prevent="submit">
    <input type="text" v-model="form.name" />
    <input type="file" @input="form.avatar = $event.target.files[0]" />
    <progress v-if="form.progress" :value="form.progress.percentage" max="100">
      {{ form.progress.percentage }}%
    </progress>
    <button type="submit">Submit</button>
  </form>
</template>

<script>
import { useForm } from '@inertiajs/inertia-vue3'

export default {
  setup () {
    const form = useForm({
      name: null,
      avatar: null,
    })

    function submit() {
      form.post('/users')
    }

    return { form, submit }
  },
}
</script>

Ref: https://inertiajs.com/file-uploads#form-data-conversion参考: https://inertiajs.com/file-uploads#form-data-conversion

i found the solution for this problem:我找到了解决这个问题的方法:

the input field must be like that:输入字段必须是这样的:

<input name="img_url" id="img_url" type="file" @input="categories.img_url = $event.target.files[0]" />

Update更新

and I figure out that I have to change the variable name because I used the same variable for the props and the data variable that I want to store it and send it to backend, so I make the props from backend categories and the data that I want to store it category .我发现我必须更改变量名称,因为我对 props 和要存储它并将其发送到后端的数据变量使用了相同的变量,所以我从后端类别和我的数据中制作了 props想要存储它的类别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM