VUE饿了么UPload组件自定义上传

代码:

1.视图:

 <el-dialog :title="dialogTitle" width="30%" :visible.sync="dialogFormVisible" :destroy-on-close="true">
            <el-form ref="fileForm" class="items-align" >
                <el-form-item>
                    <el-upload
                        :limit="1"
                        class="upload-demo"
                        accept=".xlsx, .csv, .xls"
                        action = ''
                        :auto-upload="false"
                        :on-change = "onFileChange"
                        :on-exceed = "fileExceed"
                        :on-remove="handleRemove"
                        drag
                        >
                        <i class="el-icon-upload"></i>
                        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
                        <div class="el-upload__tip" slot="tip">只能上传excel文件</div>
                    </el-upload>
                </el-form-item>
                <el-form-item><el-link type="primary" download="resourceManagementDemo.xlsx" href="excel/resourceManagementDemo.xlsx">点击下载模版</el-link></el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取消</el-button>
                <el-button type="primary" @click="fileUpload">确认</el-button>
            </div>
        </el-dialog>

最外层的el-dialog, :destroy-on-close="true",能够在关闭弹窗后销毁文件上传组件,清空文件列表。

2.JS:

onFileChange(file){
   // debugger
   this.fileList.push(file.raw)
},
fileExceed(){
  this.$alert("文件上传数量只能为1")
},
handleRemove(){
  this.fileList.pop()
},

3.文件上传POST

 fileUpload() {
            const url = '******'
            const form = new FormData();
            if(this.fileList.length==0){
                this.$alert("文件数量不能为0")
                return
            }
            form.append("file", this.fileList[0])
            const params = {
                *****
            }
            this.$axios.post(url, form, {
                params
            }).then(res=>{
                if(res.status==200&&res.data.result=="SUCCESS"){
                    setTimeout(()=>{
                        this.$alert("您的文件已导入","提示",{callback: (action) => {
                        this.fileList = []
                        this.render()
                        },})
                    },500)
                    this.dialogFormVisible = false
                }else{
                    this.$alert("导入失败,请重试")
                }
            },error=>{this.$alert("导入失败,请重试")})
        },
  • 不能传入prop, :file-list=“fileList”,传入之后on-change会捕捉on-remove事件,把删除的文件添加回fileList。
  • 必须处理on-remove事件, 否则文件没有被移出数组
  • 不能使用on-success钩子, 自定义上传该函数不生效
  • 设置不允许自动上传,:auto-upload="false"
  • action为组件自动上传的地址,必填属性,设置为空
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹邹邹邹!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值