element oss 文件上传多选,避免接口多次请求

element 上传文件时,选择多个文件会多次调用接口

template代码如下:
<el-upload
    class="upload-file"
    ref="uploadFile"
    :limit="limit"
    multiple
    show-file-list
    :http-request="uploadFile"
    :on-change="onChange"
    :data="dataObj"
    :before-upload="beforeuploadFile"
    action="123"
    :on-exceed="handleExceed"
    :file-list="fileList">
    <el-button type="primary" size="mini" :loading="btnLoading"><i class="el-icon-upload2"></i>导入文件包</el-button>
</el-upload>
js代码如下:
const OSS = require('ali-oss') // 文件上传至oss
export default {
	data () {
		return {
			limit: 10,
            fileList: [], // 文件列表
            files: [], // 当前选中的files
            btnLoading: false,
            dataObj: { // 根据需要设置
                region: 公司oss域名',
                accessKeyId: 'oss的key',
                accessKeySecret: '',
                bucket: ''
            },
            beforeUrl: '', // 可以不设置,方便文件分类
         }
	},
	methods: {
		onChange (file, fileList) {
            this.files = fileList
        },
        beforeuploadFile (file) {
            this.client = new OSS(this.dataObj)

            // const isJPG = file.type === 'image/jpeg';
            const isLt50M = file.size / 1024 / 1024 < 50
            // if (!isJPG) {
            //     this.$message.error('上传头像图片只能是 JPG 格式!');
            // }
            if (!isLt50M) {
                this.$alarm.showWarning('上传文件大小不能超过 50MB!')
            }
            return isLt50M
        },
        uploadFile (file) { // 上传
            this.btnLoading = true
            const that = this
            var userinputName = file.file.name
            const fileName =
                that.beforeUrl +
                Math.random().toString(36).substr(2) + '_' + userinputName
            async function put () {
                try {
                // object-name可以自定义为文件名(例如file.txt)或目录(例如abc/test/file.txt)的形式,实现将文件上传至当前Bucket或Bucket下的指定目录。
                    const result = await that.client.put(fileName, file.file)
                    var index = userinputName.lastIndexOf('/')
                    userinputName = userinputName.substring(
                        index + 1,
                        userinputName.length
                    ) // 文件名
                    const arr = userinputName.split('.')
                    arr && arr.splice(arr.length - 1, 1)
                    const name = arr && arr.join('.') // 后端需要没有后缀的文件名
                    that.fileList.push({ name: userinputName, fileName: name, url: result.url })
                    if (that.fileList.length === that.files.length) {
                        const files = [] // 需要上传的数组
                        that.files.forEach(item => {
                            that.fileList.forEach(data => {
                                if (item.status === 'ready' && item.name === data.name) {
                                    files.push(data)
                                }
                            })
                        })
                        that.addTaskFilesNotaskModeAjax(files) //  调用后端接口 
                    }
                } catch (e) {
                    console.log(e)
                    that.btnLoading = false
                }
            }
            put()
        },
        addTaskFilesNotaskModeAjax (fileList) { // 批量导入后端接口
            const files = fileList.map(item => ({ fileName: item.fileName, uploadFileName: item.name, uploadFileUrl: item.url }))
            const params = {
                workId: this.workId,
                files: files
            } // 后端接口参数
            addTaskFilesNotaskMode(params).then(res => {
                if (res.code === 0) {
                    this.$alarm.showSuccess(res.msg)// 提示信息
                    // 清空默认值
                    this.files = []
                    setTimeout(() => {
                        this.btnLoading = false
                    }, 500)
                }
            }).catch(err => {
                console.log(err)
                this.btnLoading = false
                // 清空默认值
                this.files = []
            })
        },
        handleExceed (files, fileList) {
            this.$message.warning(`当前限制选择 ${this.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`)
        }
	}
}

###主要流程为文件多选上传至oss, oss返回url,最后获取所有上传的url数组,再传给后端,避免多次调用后端接口

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值