当前位置:   article > 正文

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

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

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

前端具体实现:

  1. <div v-if="!uploadVideoFlag">
  2. <el-button size="small" class="upload-btn" icon="el-icon-loading">
  3. 上传中
  4. </el-button>
  5. </div>
  6. <div v-if="uploadVideoFlag">
  7. <el-upload
  8. :http-request="uploadVideo"
  9. action="#"
  10. :limit="1"
  11. :file-list="fileVideoList"
  12. accept=".mp4"
  13. >
  14. <el-button size="small" type="primary" class="upload-btn">
  15. 上传视频
  16. </el-button>
  17. <div slot="tip" class="el-upload__tip">只能上传 MP4 文件</div>
  18. </el-upload>
  19. </div>
  1. // 上传视频
  2. uploadVideo(file) {
  3. var video = file.file.name.substring(file.file.name.lastIndexOf(".") + 1);
  4. const suffix = video === "mp4";
  5. if (!suffix) {
  6. this.form.chapterUrl = undefined;
  7. this.fileVideoList = [];
  8. this.$message.warning("必须上传 MP4 格式的视频!");
  9. return;
  10. }
  11. generatePresignedUrlApi().then((res) => {
  12. this.uploadVideoFlag = false;
  13. let keyFileName = res.data.dir + this.getFileNameUUID() + "." + video;
  14. let formData = new FormData();
  15. formData.append("success_action_status", "200"); // 指定成功上传时,服务端返回状态码200,默认返回204。
  16. formData.append("policy", res.data.policy);
  17. formData.append("signature", res.data.signature);
  18. formData.append("OSSAccessKeyId", res.data.OSSAccessKeyId);
  19. formData.append("key", keyFileName); // 文件名
  20. formData.append("file", file.file); // file必须为最后一个表单域
  21. const param = {
  22. method: "POST",
  23. body: formData,
  24. };
  25. fetch(res.data.host, param)
  26. .then((data) => {
  27. this.uploadVideoFlag = true;
  28. this.$message.success("上传成功");
  29. // 视频数组:
  30. this.fileVideoList.length = 1;
  31. // 视频地址:
  32. this.form.chapterUrl = res.data.host + keyFileName;
  33. })
  34. .catch((error) => {
  35. console.error("Error:", error);
  36. });
  37. });
  38. },
  39. // 唯一uuid
  40. getFileNameUUID() {
  41. function rx() {
  42. return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  43. }
  44. return `${+new Date()}${rx()}${rx()}`;
  45. },

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/998249
推荐阅读
相关标签
  

闽ICP备14008679号