How can I pass input file with vue.js 2?

只谈情不闲聊 提交于 2019-12-13 03:46:34

问题


My vue component like this :

<template>
    <div class="modal" tabindex="-1" role="dialog">
        ...
            <div class="form-group">
                <label for="change-image">Change image</label>
                <input type="file" name="replace" v-on:change="changeImage">
            </div>
            <div class="form-group">
                <label for="alt-image">Alt attr</label>
                <input type="text" class="form-control" v-model="altImage">
            </div>
            <div class="checkbox">
                <label>
                    <input type="checkbox" v-model="mainCover"> Set Cover
                </label>
            </div>
            <button type="button" class="btn btn-success" @click="editImageProduct">
                Edit
            </button>
        ...
    </div>
</template>
<script>
    export default{
        ...
        data() { return {altImage: '', mainCover: '', imageChanged: '', image: ''}},
        methods: {
            editImageProduct(event) {
                const payload = {alt_image: this.altImage, main_cover: this.mainCover, image_changed: this.imageChanged}
                this.$store.dispatch('editImageProduct', payload)
            },
            changeImage(e) {
                var files = e.target.files || e.dataTransfer.files
                if (!files.length)
                    return;
                this.createImage(files[0])
                this.imageChanged = files[0]
            },
            createImage(file) {
                var image = new Image()
                var reader = new FileReader()
                var vm = this
                reader.onload = (e) => {
                    vm.image = e.target.result
                };
                reader.readAsDataURL(file)
            },
        } 
    }
</script>

I want to send the parameters to controller. It will go through modules, api, routes and controller

On the controller, I do like this :

public function editImage(Request $request)
{
    dd($request->all());
}

When executed, the result like this :

array:5 [

"alt_image" => "test alt"

"main_cover" => true

"image_changed" => []

]

The param image_changed is empty

Whereas on the component, I do console.log, it display the result

When I do console.log(files[0]), the result like this :

How can I solve this problem?

来源:https://stackoverflow.com/questions/44174007/how-can-i-pass-input-file-with-vue-js-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!