vue上传组件

使用了iview组件库
components文件中创建upload_file 文件
在这里插入图片描述

upload_file.vue


<template>
    <div>
        <input :id="buttonId" type="file" @change="changeAction" style="display: none;" />
        <Button icon="ios-cloud-upload-outline" type="primary" @click="triggerFileSelecter"> {{ buttonName }} </Button>
    </div>
</template>
<script>
export default { 
    props: {
        //文件选择器change事件
        changeAction: {
            type: Function
        },
        //按钮的名称
        buttonName: {
            type: String
        },
        //按钮的ID
        buttonId: {
            type: String
        },
    },
    methods: {
        triggerFileSelecter() {
            document.querySelector("#" + this.buttonId).click();
        }
    }
}
</script>
<style lang="less" scoped>

</style>

index.vue

js部分

页面中引用组件
import uploadFile from '@/components/upload_file/upload_file.vue'
声明组件
components: {
    uploadFile
},

html中使用组件

<uploadFile
	 :button-id="'user_list_upload'"
	 :change-action="upload_users_excel"
	 :button-name="'上传Execl'"
></uploadFile>

upload_users_excel ,上传方法

upload_users_excel(e) { // Execl表上传
// 点击上传获取到上传的文件
            let test_uploadfile = e.target.files;
            // console.log(test_uploadfile,'test_uploadfile');
// 创建一个空对象实例
            let formdata = new FormData()
// 添加一个新值到 FormData 对象内的一个已存在的file键中
            formdata.append("file", test_uploadfile[0])
            // console.log(formdata,'formdata');
// 页面加载蒙层
            this.$Spin.show()
// 这里是响应需求,在用户上传时,上传的表格不符合规范,会返回一个execl表格,此时需要再将返回的execl表格下载
            upload_sku_config_excel(formdata).then(async res => {
// 使用async将返回的数据转成Promise 对象
                if(res.data.type == "application/json") {
// 使用async将返回的数据转成Promise 对象
                    let retJson = await res.data.text();
                    // console.log(retJson,'---await res.data.text();');
                    retJson = JSON.parse(retJson);
                    // console.log(retJson,'---JSON.parse(retJson);');
                    console.info(retJson);
                    if (retJson && retJson.errCode != 0) {
                        this.$Message.error(retJson.errMsg)
                        this.$Spin.hide()
                        this.reload()
                        return
                    }
                    this.$Message.success("上传成功")
                    this.$Spin.hide()
                    this.reload()
                }else{
// 这里是响应需求,在用户上传时,上传的表格不符合规范,会返回一个execl表格,此时需要再将返回的execl表格下载(正常上传就不需要else部分)
                    console.log("zou下载");
                    let blob = new Blob([res.data]);
                    let url = window.URL.createObjectURL(blob);
                    let link = document.createElement('a');
                    link.href = url;
                    link.setAttribute('download', '错误信息.xlsx');
                    document.body.appendChild(link);
                    link.click();
                    this.$Message.success("错误信息正在下载")
                    this.reload()
                    this.$Spin.hide()
                }
            })
        },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值