接口上传视频和oss直传视频到阿里云组件

devtools/2024/11/23 19:42:30/

在这里插入图片描述

接口视频上传

<template><div class="component-upload-video"><el-uploadclass="avatar-uploader":action="uploadImgUrl":on-progress="uploadVideoProcess":on-success="handleUploadSuccess":limit="limit":file-list="fileList":before-upload="handleBeforeUpload":show-file-list="false":headers="headers"ref="uploadRef"><videov-if="videoForm.showVideoPath && !videoFlag":src="videoForm.showVideoPath"class="avatar video-avatar"controls="controls">您的浏览器不支持视频播放</video><!-- //i标签是上传前的那个+上传后隐藏 --><iv-else-if="!videoForm.showVideoPath && !videoFlag"class="el-icon-plus avatar-uploader-icon"></i><el-progressv-if="videoFlag == true"type="circle":percentage="videoUploadPercent"style="margin-top: 7px"></el-progress></el-upload><el-buttonv-if="isShowBtn && videoForm.showVideoPath"class="mt-20"plainround@click="handleDelete"size="small"type="primary">重新上传<i class="el-icon-upload el-icon--right"></i></el-button></div>
</template><script>
import { getToken } from "@/utils/auth";export default {props: {value: [String, Object, Array],// 图片数量限制limit: {type: Number,default: 5,},// 大小限制(MB)fileSize: {type: Number,default: 50,},// 序号indexValue: {type: Number,default: null,},// 文件类型, 例如['video/avi', 'video/wmv', 'video/rmvb']fileType: {type: Array,default: () => ["video/mp4", "video/ogg", "video/flv"],},// 是否显示提示isShowTip: {type: Boolean,default: false,},// 是否显示进度条isShowUploadVideo: {type: Boolean,default: false,},// 是否显示重新上传按钮isShowBtn: {type: Boolean,default: true,},},data() {return {number: 0,dialogVisible: false,hideUpload: false,uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的服务器地址headers: {Authorization: "Bearer " + getToken(),},fileList: [],videoForm: {showVideoPath: "", //回显的变量},duration: 0, //时长秒videoFlag: false,videoUploadPercent: 0,};},watch: {value: {handler(val) {if (val) {this.videoForm.showVideoPath = val;// 首先将值转为数组const list = Array.isArray(val) ? val : this.value.split(",");// 然后将数组转为对象数组this.fileList = list.map((item) => {if (typeof item === "string") {item = { name: item, url: item };}return item;});} else {this.fileList = [];return [];}},deep: true,immediate: true,},},computed: {// 是否显示提示showTip() {return this.isShowTip && (this.fileType || this.fileSize);},},methods: {// 上传成功回调handleUploadSuccess(res) {this.isShowUploadVideo = true;this.videoFlag = false;// this.videoUploadPercent = 0;console.log("handleUploadSuccess");//后台上传数据if (res.code == 200) {this.videoForm.showVideoPath = res.data.url; //上传成功后端返回视频地址 回显// var audioElement = new Audio(url);this.$emit("input", res.data.url, this.duration);this.$modal.msgSuccess("上传成功!");} else {this.$message.error("上传失败!");}},// 上传前loading加载handleBeforeUpload(file) {var url = URL.createObjectURL(file);var audioElement = new Audio(url);var time;var that = this;audioElement.addEventListener("loadedmetadata", function () {time = audioElement.duration; //时长为秒that.duration = time;});var fileSize = file.size / 1024 / 1024 < this.fileSize; //控制大小  修改50的值即可if (["video/mp4"].indexOf(file.type) == -1 //控制格式) {this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}视频格式文件!`);return false;}if (!fileSize) {this.$modal.msgError(`上传视频大小不能超过 ${this.fileSize} MB!`);return false;}},//进度条--------uploadVideoProcess(event, file, fileList) {//注意在data中添加对应的变量名this.videoFlag = true;console.log(file, "file", file.percentage);this.videoUploadPercent = file.percentage.toFixed(0) * 1;},// 文件个数超出handleExceed() {this.$modal.msgError(`上传视频数量不能超过 ${this.limit} 个!`);},// 上传失败handleUploadError() {this.$modal.msgError("上传视频失败,请重试");this.$modal.closeLoading();},handleDelete() {this.videoFlag = false;// 清除已上传的文件this.$refs.uploadRef.clearFiles();// 在这里可以执行其他删除操作,例如将视频地址重置为nullthis.videoForm.showVideoPath = "";},},
};
</script><style scoped lang="scss">
// .el-upload--picture-card 控制加号部分
::v-deep.hideUpload .el-upload--picture-card {display: none;
}::v-deep .el-upload--picture-card {width: 104px;height: 104px;line-height: 104px;
}::v-deep .el-upload-list--picture-card .el-upload-list__item {width: 104px;height: 104px;
}.avatar-uploader-icon {border: 1px dashed #d9d9d9 !important;
}.avatar-uploader .el-upload {border: 1px dashed #d9d9d9 !important;border-radius: 6px !important;position: relative !important;overflow: hidden !important;
}.avatar-uploader .el-upload:hover {border: 1px dashed #d9d9d9 !important;border-color: #409eff;
}.avatar-uploader-icon {font-size: 28px;color: #8c939d;width: 300px;height: 178px;line-height: 178px;text-align: center;
}.avatar {width: 300px;height: 178px;display: block;
}
</style>

oss直传视频到阿里云组件

<template><div class="component-upload-image"><el-uploadaction="":http-request="beforeUpload"class="avatar-uploader":limit="limit":on-error="handleUploadError":on-exceed="handleExceed"name="file":show-file-list="false":file-list="fileList"ref="uploadRef"><videov-if="videoForm.showVideoPath && !videoFlag":src="videoForm.showVideoPath"class="avatar video-avatar"controls="controls">您的浏览器不支持视频播放</video><!-- //i标签是上传前的那个+上传后隐藏 --><iv-else-if="!videoForm.showVideoPath && !videoFlag"class="el-icon-plus avatar-uploader-icon"></i><el-progressv-if="videoFlag == true"type="circle":percentage="videoUploadPercent"style="margin-top: 7px"></el-progress></el-upload><el-buttonv-if="isShowBtn && videoForm.showVideoPath"class="mt-20"plainround@click="handleDelete"size="small"type="primary">重新上传<i class="el-icon-upload el-icon--right"></i></el-button></div>
</template><script>
import { getOssParameter } from "./oss";export default {props: {value: [String, Object, Array],// 图片数量限制limit: {type: Number,default: 1,},// 大小限制(MB)fileSize: {type: Number,default: 5120,},fileType: {type: Array,default: () => ["video/*"],},// 是否显示提示isShowTip: {type: Boolean,default: true,},// 是否显示进度条isShowUploadVideo: {type: Boolean,default: false,},// 是否显示重新上传按钮isShowBtn: {type: Boolean,default: true,},},data() {return {dialogImageUrl: "",dialogVisible: false,hideUpload: false,baseUrl: process.env.VUE_APP_BASE_API,uploadImgUrl: process.env.VUE_APP_BASE_API + "/file/upload", // 上传的图片服务器地址fileList: [],videoForm: {showVideoPath: "", //回显的变量},videoFlag: false,videoUploadPercent: 0,isCancel: false,};},watch: {value: {handler(val) {if (val) {this.videoForm.showVideoPath = val;// 首先将值转为数组const list = Array.isArray(val) ? val : this.value.split(",");// 然后将数组转为对象数组this.fileList = list.map((item) => {if (typeof item === "string") {item = { name: item, url: item };}return item;});} else {this.fileList = [];return [];}},deep: true,immediate: true,},},computed: {// 是否显示提示showTip() {return this.isShowTip && (this.fileType || this.fileSize);},},methods: {//自定义上传方法..Upload(file, data) {let OSS = require("ali-oss");let client = new OSS({region: data.region,accessKeyId: data.accessKeyId,accessKeySecret: data.accessKeySecret,bucket: data.bucket,});// let cdnUrl = data.cdnUrl;let cdnUrl = "https://cdn-learning.cscec83.cn/";this.isCancel = false;//判断扩展名const tmpcnt = file.file.name.lastIndexOf(".");const exname = file.file.name.substring(tmpcnt + 1);//  配置路径以及文件名称const fileName = file.file.uid + "." + exname;const progress = (p, _checkpoint) => {this.videoFlag = true;this.videoUploadPercent = Number((Number(p) * 100).toFixed(1));console.log(this.isCancel);if (this.isCancel) {client.cancel();}};client.multipartUpload(fileName, file.file, {progress,// 设置并发上传的分片数量。// parallel: 4,// 设置分片大小。默认值为1 MB,最小值为100 KB。partSize: 5 * 1024 * 1024,}).then((res) => {console.log(res, "res");this.videoFlag = false;if (res.name) {this.videoForm.showVideoPath = cdnUrl + res.name;console.log(this.videoForm.showVideoPath, "fileUrl");this.$emit("input", this.videoForm.showVideoPath, this.duration);// this.loading.close();} else {this.$modal.msgError("上传视频失败,请重试");// this.loading.close();this.handleDelete();}}).catch((err) => {console.log(err);if (err.name == "cancel") {this.$message("上传取消");} else {this.$modal.msgError(err);}this.handleDelete();});},handleDelete() {this.isCancel = true;this.videoFlag = false;this.$refs.uploadRef.clearFiles();this.duration = 0;this.videoForm.showVideoPath = "";this.$emit("input", this.videoForm.showVideoPath, this.duration);// 清除已上传的文件},// 上传前loading加载beforeUpload(file) {var fileSize = file.file.size / 1024 / 1024 < this.fileSize; //控制大小  修改50的值即可console.log(file.file.type);if (this.fileType.indexOf(file.file.type) == -1 //控制格式) {this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}视频格式文件!`);return false;}if (!fileSize) {this.$modal.msgError(`上传视频大小不能超过 ${this.fileSize} MB!`);return false;}var url = URL.createObjectURL(file.file);var audioElement = new Audio(url);var time;var that = this;audioElement.addEventListener("loadedmetadata", function () {time = audioElement.duration; //时长为秒that.duration = time;});// this.loading = this.$loading({//   lock: true,//   text: "上传中",//   background: "rgba(0, 0, 0, 0.7)",// });getOssParameter().then((res) => {if (res.code == 200) {this.Upload(file, res.data);}});},// 文件个数超出handleExceed() {this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);},// 上传失败handleUploadError() {this.$modal.msgError("上传失败,请重试");// this.loading.close();},uploadCancel() {this.isCancel = true;},},
};
</script>
<style scoped lang="scss">
// .el-upload--picture-card 控制加号部分
::v-deep.hideUpload .el-upload--picture-card {display: none;
}::v-deep .el-upload--picture-card {width: 104px;height: 104px;line-height: 104px;
}::v-deep .el-upload-list--picture-card .el-upload-list__item {width: 104px;height: 104px;
}.avatar-uploader-icon {border: 1px dashed #d9d9d9 !important;
}.avatar-uploader .el-upload {border: 1px dashed #d9d9d9 !important;border-radius: 6px !important;position: relative !important;overflow: hidden !important;
}.avatar-uploader .el-upload:hover {border: 1px dashed #d9d9d9 !important;border-color: #409eff;
}.avatar-uploader-icon {font-size: 28px;color: #8c939d;width: 300px;height: 178px;line-height: 178px;text-align: center;
}.avatar {width: 300px;height: 178px;display: block;
}
</style>

http://www.ppmy.cn/devtools/136364.html

相关文章

Pytorch使用手册-Tensors(专题二)

这段代码是对 PyTorch 中张量(Tensors)的详细介绍和操作演示。以下是逐步讲解: 1. 什么是张量 (Tensor) 张量是一种专门的数据结构,与 NumPy 的多维数组(ndarray)类似: 它可以在 GPU 或其他硬件加速器上运行。张量可以与 NumPy 共享内存,避免不必要的数据拷贝。它是为…

基于 DRNN 神经网络整定的 PID 解耦控制

1. 基本原理 DRNN&#xff08;Dynamic Recurrent Neural Network, 动态递归神经网络&#xff09;是一种带有时间反馈的神经网络&#xff0c;能够建模系统的动态特性&#xff0c;适用于非线性、多变量、时变系统的控制。结合 PID 解耦控制&#xff0c;利用 DRNN 进行动态建模和…

VITE 忽略指定路径的资源

前言 问题起因是因为项目需要引入服务器端的网络图片 而在编写配置时发现&#xff0c;Vite并不支持排除指定前缀的资源 唯一可以排外的只有 Rollup 的 external 选项可以排除外部依赖&#xff0c;但他只能排除外部依赖&#xff0c;不支持指定路径资源或指定前缀的资源&#…

FreeRTOS信号量(一)

目录 什么是信号量&#xff1f; 1.信号量简介 2.二值信号量 2.1二值信号量简介 1. 首先&#xff0c;创建时&#xff0c;二值信号量默认无效 2. 之后中断释放信号量 3.信号量获取成功 4、任务再次进入阻塞态 2.2 创建二值信号量 1、函数vSemaphoreCreateBinary () 2、…

Python3.11.9+selenium,获取图片验证码以及输入验证码数字

Python3.11.9+selenium,获取图片验证码以及输入验证码数字 1、遇到问题:登录或修改密码需要验证码 2、解决办法: 2.1、安装ddddocr pip install ddddocr 2.2、解析验证码函数 import ddddocr def get_capcha_text():#获取验证码图片ele_pic = driver.find_element(By.XPAT…

FreeRTOS:事件标志组与任务通知

目录 一、事件标志组&#xff08;Event Groups&#xff09; 1、事件标志组的特点 2、事件标志组与队列、信号量的区别 3、关键API函数 4、示例代码 5、优缺点 6、总结 二、任务通知&#xff08;Task Notifications&#xff09; 1、任务通知的特点 2、关键API函数 3、…

MYSQL——数据更新

一、插入数据 1.插入完整的数据记录 在MYSQL中&#xff0c;使用SQL语句INSERT插入一条完整的记录&#xff0c;语法如下&#xff1a; INSERT INTO 表名 [(字段名1[,...字段名n])] VALUES (值1[...,值n]); 表名——用于指定要插入的数据的表名 字段名——用于指定需要插入数据…

python语言基础-5 进阶语法-5.4 正则表达式

声明&#xff1a;本内容非盈利性质&#xff0c;也不支持任何组织或个人将其用作盈利用途。本内容来源于参考书或网站&#xff0c;会尽量附上原文链接&#xff0c;并鼓励大家看原文。侵删。 5.4 正则表达式 5.4.1 正则表达式 正则表达式的概念&#xff1a; 正则表达式是用来…