UploadFiles.vue 3.73 KB
<template>
    <a-spin :tip="$t('common.uploading')" class="spin-upload" v-if="upLoading"></a-spin>
    <template v-if="type === 'button'">
        <a-upload v-model:fileList="fileList" :headers="headers" name="file" :multiple="multiple" :action="apiUrl+action" :showUploadList="showUploadList"
                  @change="handleChange" @download="download" :max-count="max" :beforeUpload="beforeUpload" @reject="handleReject" @remove="handleRemove" :accept="fileType">
            <div v-if="showButton">
                <a-button class="flex-center">
                    <upload-outlined/>
                    <span>{{ buttonName || $t('common.upload') }}</span>
                </a-button>
            </div>
        </a-upload>
        <div class="file-type" v-if="showSuffixesHint">
            <div class="text-danger">{{ $t('common.file.type') }}:{{ suffixes }}</div>
        </div>
        <div class="file-type" v-if="showMaxHint">
            <div class="text-danger">{{ $t('common.file.limit') }}:{{ max }}</div>
        </div>
    </template>
    <template v-else-if="type === 'dragger'">
        <a-upload-dragger class="dragger" v-model:fileList="fileList" :headers="headers" name="file" :max-count="max" :multiple="multiple" :action="apiUrl+action"
                          @change="handleChange" @download="download" :beforeUpload="beforeUpload" @reject="handleReject" @remove="handleRemove" :accept="fileType" @drop="handleDrop">
            <p class="ant-upload-drag-icon">
                <inbox-outlined></inbox-outlined>
            </p>
            <p class="ant-upload-text">{{ $t('common.upload_drag') }}</p>
            <span class="ant-upload-hint">
                <span class="file-type" v-if="showSuffixesHint">
                    <span class="text-danger">{{ $t('common.file.type') }}:{{ suffixes }}</span>
                </span>
                <span class="file-type" v-if="showMaxHint">
                    <span class="text-danger">{{ $t('common.file.limit') }}:{{ max }} </span>
                </span>
            </span>
        </a-upload-dragger>
    </template>
</template>

<script lang="ts">
import {InboxOutlined, UploadOutlined} from '@ant-design/icons-vue';
import {Component, Prop, Watch} from "vue-facing-decorator";
import {message} from "ant-design-vue";
import {UploadBase} from '../UploadBase';

/**
 * 创建上传的用法
 * <UploadFiles ref="uploadFiles"
 :directory="'document-process'"
 suffixes='doc,docx,pdf,xlsx,xls,jpg,png,jpeg' @change="fileChange"/>
 编辑的上传用法
 需加上:fileListProp="documentProcess?.filePath"属性
 */
@Component({
    name: "MUploadFiles",
    components: {UploadOutlined, InboxOutlined}
})
export default class UploadFiles extends UploadBase {

    /**
     * 类型
     */
    @Prop({type: String, required: false, default: 'button'})
    type: 'button' | 'dragger';


    handleDrop(e: DragEvent): void {
        console.log(e);
    }

    handleReject(fileList: any[]): void {
        let fileName = "";
        if (fileList && fileList.length > 0) {
            fileName = fileList[0].name;
        }
        message.warning(this.$t('common.upload_file_type_error', {name: fileName, suffixes: this.suffixes}));
    }

    showMessage(msg: string, type: "success" | "warning" | "error" | "info"): void {
        switch (type) {
            case "success":
                message.success(msg);
                break;
            case "warning":
                message.warning(msg);
                break;
            case "error":
                message.error(msg);
                break;
            case "info":
                message.info(msg);
                break;
        }
    }
}
</script>

<style scoped>
.dragger {
    ::v-deep(.ant-upload) {
        height: auto !important
    }
}
</style>