Ant Vue 下载模块,批量导入

<template>
    <a-modal
        title="批量导入"
        width="600px"
        :visible="visible"
        @cancel="handleCancel"
    >
        <div>
            <div>
                <label>文件模板:</label>
                <a-button type="primary" @click="download" icon="download"> 下载模板 </a-button>
            </div>
            <br />
            <div>
                <label>上传文件:</label>
                <a-upload
                    name="file"
                    :multiple="false"
                    :fileList="fileList"
                    :remove="handleRemove"
                    :beforeUpload="beforeUpload"
                >
                    <a-button> <a-icon type="upload" /> 选择文件 </a-button>
                </a-upload>
            </div>
        </div>

        <template slot="footer">
            <a-button @click="handleCancel">关闭</a-button>
            <a-button type="primary" @click="handleImport" :disabled="fileList.length === 0" :loading="uploading">
                {{ uploading ? '上传中...' : '开始上传' }}
            </a-button>
        </template>
    </a-modal>
</template>
<script>
import Vue from 'vue'
// 上传组件 header 需要存入 token
import { ACCESS_TOKEN } from '@/comment/index'
export default {
    data() {
        return {
            headers: null,
            visible: false,
            fileList: [],
            down: '下载模板接口',
            upload: '导入接口',
            uploading: false,
        }
    },
    created() {
        const token = Vue.ls.get(ACCESS_TOKEN)
        this.headers = { 'X-Access-Token': token }
    },
    methods: {
        show(name) {
            this.uploading = false
            this.visible = true
            this.fileList = []
        },
        download() {
        	window.open(this.down)
        },
        handleCancel() {
            this.visible = false
        },
        // 移除文件
        handleRemove(file) {
            const index = this.fileList.indexOf(file)
            const newFileList = this.fileList.slice()
            newFileList.splice(index, 1)
            this.fileList = newFileList
        },
        beforeUpload(file) {
   			// 保持文件数组中只能存储一个文件
            this.fileList = [file]
            return false
        },
        // 只有点击上传按钮,才能发送接口
        handleImport() {
            const { fileList } = this
            const formData = new FormData()

            fileList.forEach((file) =>  formData.append('file', file) })
            this.uploading = true
            this.$post(this.upload, formData).then((res) => {
                this.uploading = false
                if (res.success) {
                    this.visible = false
                    this.$message.success('导入上传成功')
                } else {
                    this.$message.warning('导入上传失败')
                }
            })
        },
    },
}
</script>

完整效果
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值