Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Vue和Laravel上传文件_Laravel_Laravel 5_Vue.js - Fatal编程技术网

使用Vue和Laravel上传文件

使用Vue和Laravel上传文件,laravel,laravel-5,vue.js,Laravel,Laravel 5,Vue.js,我正在尝试使用vue和laravel进行一个简单的用户头像更新,我不知道我在这里做错了什么 我有一个带有此表单和此脚本的vue组件: <form @submit.prevent="updateAvatar" action="#" method="POST" enctype="multipart/form-data"> <div class="form-group"> <label name="task-title">Inca

我正在尝试使用vue和laravel进行一个简单的用户头像更新,我不知道我在这里做错了什么

我有一个带有此表单和此脚本的vue组件:

<form @submit.prevent="updateAvatar" action="#" method="POST" enctype="multipart/form-data">
        <div class="form-group">
          <label name="task-title">Incarca poza:</label>
          <input @change="newAvatar" id="avatar" type="file" name="avatar">
        </div>
        <button type="submit" class="btn btn-primary general-task-btn">Actualizeaza</button>
      </form>

<script>
export default{
    props: ['userid'],
    data(){
        return{
            avatar: {
            }
        }
    },
    methods: {
    newAvatar(event) {
       let files = event.target.files;
       if (files.length) this.avatar = files[0];
    },
    updateAvatar: function () {
        let data = new FormData();
        data.append('file', this.avatar);
        axios.put('/api/user/updateAvatar', data)
                .then(res => {
                    console.log(res);
                 }) 
                 .catch(error => console.log(error));
        // window.location.href = "/profile";
      }
    }
}
我真正不知道的是如何访问我用axios发送的formData。服务器告诉我这是空的。 在发出put请求之前,我尝试console.log formData,但是我得到了一个空的obj,即使内容在化身对象中。当我在线阅读时,你看不到formData的内容,因此我无法判断数据是否正确发送。
也许你们可以让我明白这一点。

这似乎与这个问题有关

试着替换

let data = new FormData(); 
data.append('file', this.avatar);
data.append('_method', 'put'); // add this
axios.post('/api/user/updateAvatar',data) // change this to post )
.then(res =>{
   console.log(res); 
}) 
.catch(error => console.log(error)); //             
   window.location.href = "/profile";
}

请求标题中
什么是
内容类型
?通过调用
formData.entries()
,您完全可以看到
formData
中的内容。您也可以使用
for(pair of formData.entries()){console.log(pair)}
遍历它。只需要使用较新版本的chrome/firefox,或者在较旧的浏览器中使用polyfill。FormData是一个JS对象,您应该能够访问它的属性,阅读并确保您的标题是
“内容类型”:“多部分/表单数据”
let data = new FormData(); 
data.append('file', this.avatar);
data.append('_method', 'put'); // add this
axios.post('/api/user/updateAvatar',data) // change this to post )
.then(res =>{
   console.log(res); 
}) 
.catch(error => console.log(error)); //             
   window.location.href = "/profile";
}