繁体   English   中英

FormData Object 未收到文件

[英]FormData Object not receiving file

我正在尝试按照构建论坛视频系列创建头像编辑器。

我在 Laravel 5.8.34。

#handleFileUpload(e)# 方法中的 console.log 显示上传的文件。

上传的图像出现在页面上。

#persist(file)# 方法中的 console.log 显示一个空的 object。

数据表格数据 {}

上传不会持续。

我的 Controller 方法:

public function avatar_upload($id)
    {
        $validate = request()->validate([
            'avatar' => ['required', 'image']
        ]);

        $emp = Employee::with('user')->where('user_id', $id)->first();
        $avatar = $emp->user->firstName . $emp->user->lastName . '.png';

        Storage::disk('spaces')
            ->putFileAs('avatars', request()->file('avatar'), $avatar, 'public');

        $emp->avatar = $avatar;
        $emp->save();

        return response([], 204);
    } // end function

我的组件:

<template>
    <div>
        <div class="text-center mb-4">

            <div class="flex justify-center font-thin text-grey-dark text-2xl">
                {{user.office}}
            </div>

            <div class="text-center">
                <img class="relative rounded-lg"
                    :src="avatar">
            </div>
            <form @submit.prevent="handleFileUpload"
                  enctype="multipart/form-data"
                  v-if="canEdit">
                <input
                    type="file"
                    name="avatar"
                    ref="file"
                    accept="image/png"
                    class="tw-input"
                    @change="handleFileUpload">
            </form>
        </div>
    </div>
</template>

<script type="text/babel">
    export default {
        name: 'AvatarReplace',
        data() {
            return {
                canEdit: true,
                avatar: this.user.avatar
            };
        },
        props: ['user'],
        methods: {
            handleFileUpload(e) {
                if(! e.target.files.length) { return; } // end if

                let file = e.target.files[0];
                console.log('FILE', file);

                let reader = new FileReader();

                reader.readAsDataURL(file);

                reader.onload = e => {
                  this.avatar = e.target.result;
                };

                this.persist(file);
            },
            persist(file) {
                let data = new FormData();

                data.append('avatar', file);
                console.log('DATA', data);

                let path = `/api/staff/avatar_upload/${this.user.id}`;
                axios.post(path, data)
                    .then((rsp) => {
                        //console.log(rsp);
                        //this.$toastr.s('File Uploaded');
                    });

            }
        }
    };
</script>

这不是正常形式,Make axios 知道content-typemultipart/form-data

axios.post(path, data, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
}).then((response) => {
   //
});

暂无
暂无

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

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