Vuetify File Uploads

二次信任 提交于 2019-12-08 17:20:42

This following code works fine for me. I've used axois for HTTPClient you might choose anything

<div id="app">
   <v-btn color="blue-grey" class="black--text" @click.native="openFileDialog">
    Upload
    <v-icon right dark> cloud_upload</v-icon>
   </v-btn>
   <input type="file" id="file-upload" style="display:none" @change="onFileChange">
</div> 


Vue.use(Vuetify);
var vm = new Vue({
    el: "#app",
    data: {
        formData: new FormData(),
    },
    methods: {
        openFileDialog() {
            document.getElementById('file-upload').click();
        },
        onFileChange(e) {
            var self = this;
            var files = e.target.files || e.dataTransfer.files;       
            if(files.length > 0){
                for(var i = 0; i< files.length; i++){
                    self.formData.append("file", files[i], files[i].name);
                }
            }   
        },
        uploadFile() {
            var self = this; 
            axios.post('URL', self.formData).then(function (response) {
                console.log(response);
            }).catch(function (error) {
                console.log(error);
            });
        },
    },

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