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是由后端提供的接口 来获取密钥

相关推荐
天蓝色的鱼鱼1 分钟前
你的项目真的需要SSR吗?还是只是你的简历需要?
前端·架构
恋猫de小郭28 分钟前
移动端开发稳了?AI 目前还无法取代客户端开发,小红书的论文告诉你数据
前端·flutter·ai编程
文心快码BaiduComate1 小时前
百度云与光本位签署战略合作:用AI Agent 重构芯片研发流程
前端·人工智能·架构
闲云一鹤1 小时前
nginx 快速入门教程 - 写给前端的你
前端·nginx·前端工程化
QCY2 小时前
「完全理解」1 分钟实现自己的 Coding Agent
前端·agent·claude
一拳不是超人2 小时前
Electron主窗口弹框被WebContentView遮挡?独立WebContentView弹框方案详解!
前端·javascript·electron
anyup2 小时前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
雮尘3 小时前
如何在非 Claude IDE (TARE、 Cursor、Antigravity 等)下使用 Agent Skills
前端·agent·ai编程
icebreaker3 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker3 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序