iview手动上传文件

问题描述:在使用iview中upload上传文件时,默认选完所需上传文件之后,就会立刻上传,但有时候还需要做一些判断才会上传

<template>
    <div>
        <Upload
            :before-upload="handleUpload"
            action="//jsonplaceholder.typicode.com/posts/">
            <Button icon="ios-cloud-upload-outline">Select the file to upload</Button>
        </Upload>
        <div v-if="file !== null">Upload file: {{ file.name }} <Button type="text" @click="upload" :loading="loadingStatus">{{ loadingStatus ? 'Uploading' : 'Click to upload' }}</Button></div>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                file: null,
                loadingStatus: false
            }
        },
        methods: {
            handleUpload (file) {
                this.file = file;
                return false;
            },
            upload () {
                this.loadingStatus = true;
                setTimeout(() => {
                    this.file = null;
                    this.loadingStatus = false;
                    this.$Message.success('Success')
                }, 1500);
            }
        }
    }
</script>

上面是iview官方示例代码,upload方法并没有实际上传

解决方法;取消默认上传行为:在before-upload方法中,返回false,即可阻止默认上传,但是同时也传不了了

     此时需要在,upload方法中添加一行代码:this.$refs.upload.post("this.file");

注意:在添加代码之前将this.file = null去掉,不然每次上传报错

posted @ 2019-02-21 16:34  nixin  阅读(2102)  评论(0)    收藏  举报