el-upload使用自定义上传,获取上传进度和上传成功回调

项目场景:

在后台管理系统上传图片时,使用自定义的上传方法


问题描述

用element-ui + vue2.0上传多张图片,并使用自定义方法时,无法获取上传进度和上传完成后的图片列表,想要获取上传完成之后的列表

<el-upload :action="actionUrl"
                     multiple
                     ref="uploadPic"
                     accept="image/jpeg, image/png, image/jpg"
                     list-type="picture-card"
                     :http-request="(e) => {uploadFile(e, row)}"
                     :on-success="(response, file, fileList) => {uploadSuccess(fileList, row)}"
                     :on-remove="(file, fileList) => {removeFileList(fileList, row)}"
                     :file-list="row.imageCos || []"
          >
    <i class="el-icon-plus"></i>
 </el-upload>
	uploadFile({file, onProgress, onSuccess, beforeUpload}, row) {
      uploadPic(file, progress => {
        onProgress(progress)
      }).then(res => {
        onSuccess({
          name: res.name,
          url: res.url,
        })
      })
    },
    uploadSuccess(fileList, row) {
      this.$set(row, 'imageCos', fileList)
    },
    removeFileList(fileList, row) {
      this.$set(row, 'imageCos', fileList)
    },

原因分析:

看el-upload的源码得知,在自定义的http-request的函数中,有以下几个传参

post(rawFile) {
      const { uid } = rawFile;
      const options = {
        headers: this.headers,
        withCredentials: this.withCredentials,
        file: rawFile,
        data: this.data,
        filename: this.name,
        action: this.action,
        onProgress: e => {
          this.onProgress(e, rawFile);
        },
        onSuccess: res => {
          this.onSuccess(res, rawFile);
          delete this.reqs[uid];
        },
        onError: err => {
          this.onError(err, rawFile);
          delete this.reqs[uid];
        }
      };
      const req = this.httpRequest(options);
      this.reqs[uid] = req;
      if (req && req.then) {
        req.then(options.onSuccess, options.onError);
      }
    },

headers、file、filename、data、action、onProgress、onSuccess、onError这几个参数可以获取,调用onProgress和onSuccess就可以实现上传进度和上传成功监听


解决方案:

上传时调用onPressgress回调,上传成功后调用onSuccess回调,在调用el-upload on-success属性,获取上传成功的列表。

<el-upload :action="actionUrl"
                     multiple
                     ref="uploadPic"
                     accept="image/jpeg, image/png, image/jpg"
                     list-type="picture-card"
                     :http-request="(e) => {uploadFile(e, row)}"
                     :on-success="(response, file, fileList) => {uploadSuccess(fileList, row)}"
                     :on-remove="(file, fileList) => {removeFileList(fileList, row)}"
                     :file-list="row.imageCos || []"
          >
    <i class="el-icon-plus"></i>
 </el-upload>
uploadFile({file, onProgress, onSuccess, beforeUpload}, row) {
      uploadPic(file, progress => {
        onProgress(progress)
      }).then(res => {
        onSuccess({
          name: res.name,
          url: res.url,
        })
      })
    },
    uploadSuccess(fileList, row) {
      this.$set(row, 'imageCos', fileList)
    },
    removeFileList(fileList, row) {
      this.$set(row, 'imageCos', fileList)
    },
对于 `el-upload` 组件的自定义上传进度,你可以通过监听 `change` 事件来获取上传的文件对象,并使用自定义的方法来获取上传进度。 首先,在 `el-upload` 上添加 `@change` 事件的监听器,例如: ```html <el-upload class="upload-demo" action="/your-upload-url" :on-change="handleChange" > <el-button slot="trigger" size="small" type="primary">选取文件</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> ``` 然后,在 Vue 实例中定义 `handleChange` 方法来处理文件的上传进度,例如: ```javascript export default { methods: { handleChange(file, fileList) { // 使用自定义的方法获取上传进度 this.getUploadProgress(file); }, getUploadProgress(file) { // 自定义获取上传进度的方法 const xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", (event) => { if (event.lengthComputable) { const progress = Math.floor((event.loaded / event.total) * 100); console.log(`Upload progress: ${progress}%`); // 在这里更新上传进度的显示或执行其他操作 } }); xhr.upload.addEventListener("load", (event) => { console.log("Upload complete"); // 在这里执行上传完成后的操作 }); // 发送请求 xhr.open("POST", "/your-upload-url"); xhr.setRequestHeader("Content-Type", "multipart/form-data"); xhr.send(file); } } } ``` 上述代码中,`getUploadProgress` 方法使用 `XMLHttpRequest` 对象来发送文件上传请求,并通过监听 `progress` 事件获取上传进度,然后在事件处理函数中更新上传进度的显示或执行其他操作。 请根据你的实际需求修改代码,并将 `/your-upload-url` 替换为你的上传接口地址。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值