Element-UI 多个el-upload组件自定义上传,不用上传url,并且携带自定义传参(文件序号)

1. 需求: 有多个(不确定具体数量)的upload组件,每个都需要单独上传获取文件(JS File类型),不需要action上传到指定url,自定义上传动作和http操作。而且因为不确定组件数量,所以每次也需要获取是第几个文件(索引),所以也需要实现附加索引这个参数

在这里插入图片描述

2. 实现:如下

<el-col v-bind="grid2" v-for="(item, index) in list" :key="index"> # list 不知道一共有几个列表项
    <el-form-item required :label="item.value" prop="randomAmount">
        <el-upload
            class="upload-demo"
            action="none" #这里随便写
            :http-request="handleFileUpload" #这里会覆盖原本的上传http的操作,从而实现自定义。
            :limit="1"
            :on-remove="(file, fileList)=> {return onRemove(file, fileList, index)}" #这里的index是自定义索引参数,这种写法能够携带自己的参数
            :on-change="(file, fileList)=> {return onChange(file, fileList, index)}">  
            <el-button round>点击上传</el-button>
        </el-upload>
    </el-form-item>
</el-col>
<el-col :span="24">
    <el-button type="primary" @click="submitForm">确认提交</el-button>
</el-col>


<script>
export default {
	data() {
		return {
            // 暂存个el-upload的File
            fileUploaded: {
                1: null,
                2: null,
                3: null,
                4: null,
                5: null // 可以多写几个(确定最多不会上传超过某数量的文件)
            },
			list:[ // 根据这个列表,渲染相应数量的el-upload组件
				{ key: "1", value: "个体工商户/企业营业执照照片" },
				{ key: "2", value: "政府机关/事业单位/社会组织登记证书照片" },
				// list 不知道一共有几个列表项,这部分是通过后端请求获取的
			]
		}
	},
	methods: {
        // 覆盖默认的http行为
        handleFileUpload(options, index){
        },
        // 文件操作删除
        onRemove(file, fileList, index){
            this.fileUploaded[index] = null
        },
        // 文件上传
        onChange(file, fileList, index) {
            // 判断上传的文件是否是满足格式要求(自定义)
            if(!file.name.includes('.zip')) {
                fileList.pop() # 清除上传文件后展示出来的文件图标
                return this.$message.error("请上传zip压缩包!")
            }
            // 判断上传的文件是否超过大小限制(自定义)
            if (file.size >= 5*1024*1024){ // 5mb
                fileList.pop()
                return this.$message.error("大小不能超过5MB!")
            }
            // 判断上传的文件的状态(一般不会出错)
            if(file.status != 'ready'){
                fileList.pop()
                return this.$message.error("状态错误")
            }
            // 暂存文件
            this.fileUploaded[index] = file.raw
        },
        // 表单上传,先判断文件是否按要求上传了,满足要求的话,构造formData
        submitForm(){
            let formdata = new FormData()
            for (let i = 0; i < this.list.length; i++) {
            	// 如果有文件未上传,则报错。确保每个el-upload都上传了文件
                if (!this.fileUploaded[i]){
                    return this.$message.error('请上传'+this.list[i].value)
                }
                formdata.append('subMerCertFiles', this.fileUploaded[i]) // 多个文件上传
            }
            // 后面调用接口,上传formdata
            
       	}
    }
}
</script>

演示

在这里插入图片描述
在这里插入图片描述

  • 24
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,关于 element-ui上传组件 el-upload,您可以通过自定义 el-upload-list 来实现自定义上传列表的功能。 具体实现方法如下: 1. 在 el-upload 组件中添加属性 list-type="picture-card",并设置属性 action 为上传文件的地址。 2. 在 el-upload 组件中添加自定义 slot,用于自定义上传列表的显示。 3. 在自定义上传列表组件中,使用 el-upload-list 组件来展示上传文件的列表。 4. 可以通过监听 el-upload 组件的 change 事件来获取上传文件信息,并将上传文件信息传递给自定义上传列表组件,以展示上传文件的列表。 以下是一个简单的示例代码: ```html <template> <div> <el-upload class="upload-demo" action="/upload" list-type="picture-card" :on-change="handleChange" > <i class="el-icon-plus"></i> <div class="upload-text">上传图片</div> </el-upload> <custom-upload-list :file-list="fileList"></custom-upload-list> </div> </template> <script> import CustomUploadList from './CustomUploadList.vue'; export default { components: { CustomUploadList, }, data() { return { fileList: [], }; }, methods: { handleChange(file, fileList) { this.fileList = fileList; }, }, }; </script> ``` CustomUploadList.vue 组件的代码如下: ```html <template> <el-upload-list class="custom-upload-list" :file-list="fileList"> <template slot-scope="{file}"> <div class="custom-list-item"> <img :src="file.url" class="custom-list-item-thumbnail"> <div class="custom-list-item-name">{{ file.name }}</div> <div class="custom-list-item-actions"> <el-button size="small" type="text" @click="handleRemove(file)">删除</el-button> </div> </div> </template> </el-upload-list> </template> <script> export default { props: { fileList: { type: Array, default: () => [], }, }, methods: { handleRemove(file) { const index = this.fileList.indexOf(file); if (index !== -1) { this.fileList.splice(index, 1); } }, }, }; </script> ``` 希望这个示例能够帮助到您。如果您有任何问题,请随时提出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值