upload-file.vue 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
<template>
    <div class="upload-file">
        <upload 
          :action="url" 
          :headers="headers ? headers : null"
          :multiple="multiple === true ? true : false" 
          :paste="paste === false ? false : true"
          :disabled="disabled === true ? true : false" 
          :data="data ? data : null"
          :type="type === 'select' ? 'select' : 'drag'" 
          :show-upload-list="showuploadlist === true ? true : false"
          :accept="accept ? accept : null" 
          :format="format ? format : []"
          :maxsize="maxsize ? maxsize : null" 
          :on-before-upload="beforeupload" 
          :on-success="success"
          :on-error="error" 
          :on-remove="remove" 
          :on-format-error="formaterror"
          :on-exceeded-size="exceededsize" 
          :on-progress="progress">
            <div class="upload-text">
                <p>{{$t('components.uploadFile.imgMsg')}}</p>
                <p><span class="text-style">{{$t('components.uploadFile.localUpload')}}</span>{{$t('components.uploadFile.or')}}<span class="text-style">{{$t('components.uploadFile.imgMsg1')}}</span></p>
            </div>
        </upload>
    </div>
</template>

<script lang="ts">
import { Vue, Component, Prop, Emit } from 'vue-property-decorator';

@Component({})
export default class UploadFile extends Vue {

    /**
     * 上传路径
     * @type {any}
     * @memberof UploadFile
     */
    public url: string = '//jsonplaceholder.typicode.com/posts/';

    /**
     * 设置上传的请求头部
     * @type {Object}
     * @memberof UploadFile
     */
    @Prop() public headers?: Object;

    /**
     * 是否支持多选文件
     * @type {boolean}
     * @memberof UploadFile
     */
    @Prop() public multiple?: boolean;

    /**
     * 是否支持粘贴上传文件
     * @type {boolean}
     * @memberof UploadFile 
     */
    @Prop() public paste?: boolean;

    /**
     * 是否禁用
     * @type {boolean}
     * @memberof UploadFile 
     */
    @Prop() public disabled?: boolean;

    /**
     * 上传时附带的额外参数
     * @type {any}
     * @memberof UploadFile 
     */
    @Prop() public data?: any;

    /**
     * 上传控件的类型,可选值为 select(点击选择),drag(支持拖拽)
     * @type {any}
     * @memberof UploadFile 
     */
    @Prop() public type?: any;

    /**
     * 是否显示已上传文件列表
     * @type {boolean}
     * @memberof UploadFile
     */
    @Prop() public showuploadlist?: boolean;

    /**
     * 接受上传的文件类型
     * @type {string}
     * @memberof UploadFile
     */
    @Prop() public accept?: string;

    /**
     * 支持的文件类型,与 accept 不同的是,format 是识别文件的后缀名,accept 为 input 标签原生的 accept 属性,
     * 会在选择文件时过滤,可以两者结合使用
     * @type {Array<any>}
     * @memberof UploadFile
     */
    @Prop() public format?: Array<any>;

    /**
     * 文件大小限制,单位 kb
     * @type {number}
     * @memberof UploadFile
     */
    @Prop() public maxsize?: number;

    /**
     * 上传文件之前的钩子,参数为上传的文件,若返回 false 或者 Promise 则停止上传
     */
    @Emit()
    public beforeupload(file:any) {
        console.log('beforeupload---------'+file);
    }

    /**
     * 文件上传成功时的钩子,返回字段为 response, file, fileList
     */
    @Emit()
    public success(response: any, file: any, fileList: any) {
        console.log('success---------' + file);
    }

    /**
     * 文件上传时的钩子,返回字段为 event, file, fileList
     */
    @Emit()
    public progress(event: any, file: any, fileList: any) {
        console.log('progress---------' + file);
    }

    /**
     * 文件上传失败时的钩子,返回字段为 error, file, fileList
     */
    @Emit()
    public error(error: any, file: any, fileList: any) {
        console.log('error---------' + file);
    }

    /**
     * 文件列表移除文件时的钩子,返回字段为 file, fileList
     */
    @Emit()
    public remove(file: any, fileList: any) {
        console.log('remove---------' + file);
    }

    /**
     * 文件格式验证失败时的钩子,返回字段为 file, fileList
     */
    @Emit()
    public formaterror(file: any, fileList: any) {
        console.log('formaterror---------' + file);
    }

    /**
     * 文件超出指定大小限制时的钩子,返回字段为 file, fileList
     */
    @Emit()
    public exceededsize(file: any, fileList: any) {
        console.log('exceededsize---------' + file);
    }

}
</script>

<style lang='less'>
@import './upload-file.less';
</style>