基于ElementUI自定义上传附件

<el-upload style="display: inline-block" :limit="6"
    class="upload-demo" action="#" ref="upload" multiple
    :file-list="fileList" :on-change="handleChange"
    :on-exceed="handleExceed" :before-upload="beforeAvatarUpload"
    :http-request="uploadSectionFile" :auto-upload="false"
    :on-remove="handleRemoveAttachment" :before-remove="handleRemove">
    <span slot="trigger" class="selectFile">选取文件</span>  </el-upload>
    <el-button @click='uploadFiles'>上传<el-button>
    
     //附件上传
            // 上传前校验
            beforeAvatarUpload(file) {
                console.log(file)
                const isLt2M = file.size / 1024 / 1024 < 5
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 5MB!')
                }
                return isLt2M
            },
            // 超过上传limit提示
            handleExceed(files, fileList) {
                this.$message.warning(`最多上传 ${this.limit} 个文件`)
            },
            //文件删除提示
            handleRemove(file) {
                return this.$confirm(`确定移除 ${file.name}?`).then(() => {
                    this.$message.success('删除')
                })
            },
            //根据新增 删除还是编辑 删除 编辑的附件是存到数据库的需要调接口
            handleRemoveAttachment(file, fileList) {
                console.log(file)
                if (this.resourceForm.id) {
                    delAttach({ id: file.id }).then((res) => {
                        if (res.data.status === 1) {
                            this.$message.success('删除成功!')
                            this.getTablelist()
                        }
                    })
                } else {
                    for (var i = 0; i < this.fileList.length; i++) {
                        if (file.uid == this.fileList[i].uid) {
                            this.fileList.splice(i, 1)
                        }
                    }
                }
            },
            handleChange(file, fileList) {
                this.fileList = fileList
                console.log(this.fileList)
            },
           
            //文件上传接口
            uploadSectionFile(param) {

      //附件
                let formData = new FormData()
                var fileObj = param.file
                formData.append('files', fileObj)

       //后端需要的参数
                formData.append('attachTable', 'device')
                formData.append('tableId', this.fileId)

//不要加{  formData } !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                fileUploads(formData).then((res) => {
                    if (res.data.status === 1) {
                    } else {
                        this.$message.error('附件上传失败!')
                    }
                })
            },

// 上传
            uploadFile() {
                this.$refs.upload.submit()
            },
         

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值