vue2前端阿里云oss服务端签名直传

官方地址:如何进行服务端签名直传_对象存储(OSS)-阿里云帮助中心

前端具体实现:

<div v-if="!uploadVideoFlag">
    <el-button size="small" class="upload-btn" icon="el-icon-loading">
        上传中
    </el-button>
</div>
<div v-if="uploadVideoFlag">
    <el-upload
        :http-request="uploadVideo"
        action="#"
        :limit="1"
        :file-list="fileVideoList"
        accept=".mp4"
        >
            <el-button size="small" type="primary" class="upload-btn">
                上传视频
            </el-button>
            <div slot="tip" class="el-upload__tip">只能上传 MP4 文件</div>
    </el-upload>
</div>
// 上传视频
    uploadVideo(file) {
      var video = file.file.name.substring(file.file.name.lastIndexOf(".") + 1);
      const suffix = video === "mp4";
      if (!suffix) {
        this.form.chapterUrl = undefined;
        this.fileVideoList = [];
        this.$message.warning("必须上传 MP4 格式的视频!");
        return;
      }
      generatePresignedUrlApi().then((res) => {
        this.uploadVideoFlag = false;
        let keyFileName = res.data.dir + this.getFileNameUUID() + "." + video;
        let formData = new FormData();
        formData.append("success_action_status", "200"); // 指定成功上传时,服务端返回状态码200,默认返回204。
        formData.append("policy", res.data.policy);
        formData.append("signature", res.data.signature);
        formData.append("OSSAccessKeyId", res.data.OSSAccessKeyId);
        formData.append("key", keyFileName); // 文件名
        formData.append("file", file.file); // file必须为最后一个表单域
        const param = {
          method: "POST",
          body: formData,
        };
        fetch(res.data.host, param)
          .then((data) => {
            this.uploadVideoFlag = true;
            this.$message.success("上传成功");
            // 视频数组:
            this.fileVideoList.length = 1;
            // 视频地址:
            this.form.chapterUrl = res.data.host + keyFileName;
          })
          .catch((error) => {
            console.error("Error:", error);
          });
      });
    },

    // 唯一uuid
    getFileNameUUID() {
      function rx() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
      }
      return `${+new Date()}${rx()}${rx()}`;
    },

generatePresignedUrlApi是由后端提供的接口 来获取密钥

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值