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

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

前端具体实现:

html 复制代码
<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>
javascript 复制代码
// 上传视频
    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是由后端提供的接口 来获取密钥

相关推荐
Flynt26 分钟前
别写拖拽行为,只声明"能拖到哪":UIST 最佳论文的拖拽方案我实测了
前端·svg·preact
Moment1 小时前
代码都能让 Agent 写了,我们为什么还要学前端?
前端·后端·面试
Hilaku1 小时前
MCP 协议会成为前端 AI 工程的标准接口吗?
前端·javascript·程序员
Zan1 小时前
Tauri 2 实战:为什么一个可靠的桌面应用,应该由 Rust Host 管理生命周期?
前端·架构·rust
SameX2 小时前
鸿蒙 ArkTS 独立开发者的项目结构 model / dao / viewmodel / page / common 的分层
前端
阿星AI工作室2 小时前
WorkBuddy做数字人口播:动效包装+克隆声音+真实照片+对口型全流程
前端
火山引擎开发者社区2 小时前
给企业配一双 AI 的手:火山引擎 OS Agent 替你操作手机和电脑
前端·人工智能
三年三月2 小时前
自建https证书SSL
前端
ClouGence3 小时前
Web 自动化测试总是超时?聊聊等待、超时和异步加载
前端·测试
RobinDevNotes4 小时前
从 Electron 到 GPUI,9 个跨平台桌面方案该怎么选
前端·javascript·rust·electron·openresty·前端开发工具