Element oss分片文件上传

 <el-upload
      class="upload-demo"
      action=""
      ref="upload"
      :file-list="fileList"
      :limit="5"
      :on-change="handleChange"
      :on-remove="handleRemove"
      :auto-upload="false"
    >
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button
        style="margin-left: 10px"
        size="small"
        type="success"
        @click="submitForm"
        ref="btn"
        >上传到服务器</el-button
      >
      <el-button
        style="margin-left: 10px"
        size="small"
        type="success"
        @click="resumeUpload"
        >继续</el-button
      >
      <el-button
        style="margin-left: 10px"
        size="small"
        type="success"
        @click="stopUplosd"
        >暂停</el-button
      >
      <el-button
        style="margin-left: 10px"
        size="small"
        type="success"
        @click="abortMultipartUpload"
        >清除切片</el-button
      >
    </el-upload>
    <el-progress :percentage="percentage" :status="uploadStatus"></el-progress>




let OSS = require("ali-oss"); // 引入ali-oss插件
const client = new OSS({
  region: "", //根据那你的Bucket地点来填写
  accessKeyId: "", //自己账户的accessKeyId
  accessKeySecret: "", //自己账户的accessKeySecret
  bucket: "", //bucket名字
  secure: true // 上传链接返回支持https
});
export default {
  data() {
    return {
      fileList: [],
      file: null,
      tempCheckpoint: null, // 用来缓存当前切片内容
      uploadId: "",
      uploadStatus: null, // 进度条上传状态
      percentage: 0, // 进度条百分比
      uploadName: "", //Object所在Bucket的完整路径
    };
  },
  mounted() {
    // window.addEventListener('online', this.resumeUpload);
  },
  methods: {
    // 点击上传至服务器
    submitForm(file) {
      console.log(444444);
      this.multipartUpload();
    },
    // 取消分片上传事件
    async abortMultipartUpload() {
      window.removeEventListener("online", this.resumeUpload);
      const name = this.uploadName; // Object所在Bucket的完整路径。
      const uploadId = this.upload; // 分片上传uploadId。
      const result = await client.abortMultipartUpload(name, uploadId);
      console.log(result, "=======清除切片====");
    },
    // 暂停分片上传。
    stopUplosd() {
      window.removeEventListener("online", this.resumeUpload); // 暂停时清除时间监听
      let result = client.cancel();
      console.log(result, "---------暂停上传-----------");
    },
    // 切片上传
    async multipartUpload() {
      if (!this.file) {
        this.$message.error("请选择文件");
        return;
      }
      this.uploadStatus = null;
      // console.log("this.uploadStatus",this.file, this.uploadStatus);

      this.percentage = 0;
      try {
        //object-name可以自定义为文件名(例如file.txt)或目录(例如abc/test/file.txt)的形式,实现将文件上传至当前Bucket或Bucket下的指定目录。
        let result = await client.multipartUpload(this.file.name, this.file, {
          headers: {
            "Content-Disposition": "inline",
            "Content-Type": this.file.type, //注意:根据图片或者文件的后缀来设置,我试验用的‘.png'的图片,具体为什么下文解释
          },
          progress: (p, checkpoint) => {
            this.tempCheckpoint = checkpoint;
            this.upload = checkpoint.uploadId;
            this.uploadName = checkpoint.name;
            this.percentage = p * 100;
            // console.log(p, checkpoint, this.percentage, '---------uploadId-----------')
            // 断点记录点。浏览器重启后无法直接继续上传,您需要手动触发上传操作。
          },
          meta: { year: 2020, people: "dev" },
          mime: this.file.type,
        });
        console.log(result, this.percentage, "result= 切片上传完毕=");
        console.log(this.fileList);
      } catch (e) {
        window.addEventListener("online", this.resumeUpload); // 该监听放在断网的异常处理
        // 捕获超时异常。
        if (e.code === "ConnectionTimeoutError") {
          // 请求超时异常处理
          this.uploadStatus = "exception";
          console.log("TimeoutError");
          // do ConnectionTimeoutError operation
        }
        // console.log(e)
      }
    },
    // 恢复上传。
    async resumeUpload() {
      window.removeEventListener("online", this.resumeUpload);
      if (!this.tempCheckpoint) {
        this.$message.error("请先上传");
        return;
      }
      this.uploadStatus = null;
      try {
        let result = await client.multipartUpload(this.file.name, this.file, {
          headers: {
            "Content-Disposition": "inline",
            "Content-Type": this.file.type, //注意:根据图片或者文件的后缀来设置,我试验用的‘.png'的图片,具体为什么下文解释
          },

          progress: (p, checkpoint) => {
            this.percentage = p * 100;
            console.log(
              p,
              checkpoint,
              "checkpoint----恢复上传的切片信息-------"
            );
            this.tempCheckpoint = checkpoint;
          },
          checkpoint: this.tempCheckpoint,
          meta: { year: 2020, people: "dev" },
          mime: this.file.type,
        });
        console.log(result, "result-=-=-恢复上传完毕");
      } catch (e) {
        console.log(e, "e-=-=-");
      }
    },

    // 选择文件发生改变
    handleChange(file, fileList) {
      this.fileList = fileList.filter((row) => row.uid == file.uid);
      this.file = file.raw;
      console.log(file, fileList);
      // 文件改变时上传
      // this.submitForm(file)
      this.$refs.btn.$el.click()
    },
    handleRemove(file, fileList) {
      this.percentage = 0; //进度条置空
      this.fileList = [];
    },
  },
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
阿里云OSS分片文件上传是一种将大文件分成多个数据块(Part)进行上传的方法,以实现高效的文件上传和断点续传功能。这种分片上传的方法可以应用于需要传输大文件的场景,比如视频点播文件等。 分片上传的步骤包括初始化一个分片上传事件、上传分片和完成分片上传。首先,需要初始化一个分片上传事件,获取一个唯一的上传ID和用于标识上传的文件名。然后,将文件分成多个数据块,逐个上传每个数据块,并记录每个数据块的编号和ETag。最后,通过调用完成分片上传的接口,将所有上传的数据块合并成一个完整的文件。 在分片上传过程中,可以通过监控上传进度,实时获取上传的数据块数目和总数据块数,从而了解上传的进度情况。可以使用两种方式来实现监控上传进度,一种是通过查询上传事件的接口来获取已经上传的数据块数目和总数据块数,另一种是通过每个数据块上传完成时的回调来更新上传进度。 在使用分片上传时,需要注意一些事项。首先,每个数据块的大小可以根据实际需求进行调整,但要遵循OSS的限制条件。其次,上传数据块时需要保证数据块的顺序和完整性,以确保数据块可以正确地组合成完整的文件。最后,完成分片上传后,需要注意保存上传ID和ETag,以便之后可以进行断点续传操作。 总结来说,阿里云OSS分片文件上传是一种将大文件分成多个数据块进行上传的方法,可以实现高效的文件上传和断点续传功能。通过分片上传,可以有效地应对大文件传输的需求,并且通过监控上传进度和注意事项,可以更好地控制和管理文件上传的过程。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [阿里云对象存储OSS-分片上传](https://blog.csdn.net/Morris_/article/details/102813989)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值