当前位置: 首页> 健康> 养生 > vue2前端阿里云oss服务端签名直传

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

时间:2025/7/11 18:06:48来源:https://blog.csdn.net/weixin_74285634/article/details/141166588 浏览次数:0次

官方地址:如何进行服务端签名直传_对象存储(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);});});},// 唯一uuidgetFileNameUUID() {function rx() {return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);}return `${+new Date()}${rx()}${rx()}`;},

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

关键字:vue2前端阿里云oss服务端签名直传

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: