当前位置: 首页 > news >正文

el-upload上传图片,视频可获取视频时长。

对element-ui组件的upload组件再一次封装,简单记录。下面是效果图。

注意点:该组件现在仅支持单图和单个视频上传。

<template><div :style="myStyle"><divclass="uploads":style="{width: upWith + 'px',height: upHeight + 'px',borderRadius: upBradius + 'px',}"><div v-if="pathUrl" class="isblock"><div v-show="utype == 1"><el-imageref="preview":style="{width: upWith + 'px',height: upHeight + 'px',borderRadius: upBradius + 'px',}":preview-src-list="[pathUrl]":src="pathUrl"></el-image><div class="imgs_prews"><divclass="imgs_prew":style="{width: upWith + 'px',height: upHeight + 'px',borderRadius: upBradius + 'px',}"><i @click="ylimg()" class="el-icon-view"></i><span></span><i @click="deleteimg()" class="el-icon-delete"></i></div></div></div><div v-show="utype == 2" style="display: flex"><div style="position: relative"><video:style="{width: upWith + 'px',height: upHeight + 'px',borderRadius: upBradius + 'px',}":src="pathUrl"controlspreload="auto"playsinline="true"></video></div><div style="position: absolute; right: 0px"><el-button @click="deleteimg()" size="mini">重新上传</el-button></div></div></div><el-uploadv-elseclass="avatar-uploader":style="{width: upWith + 'px',height: upHeight + 'px',borderRadius: upBradius + 'px',lineHeight: upHeight + 'px',}":action="updatehttp":headers="headers":data="updataimg"accept=".jpg, .jpeg, .png, .gif, .bmp, .JPG, .JPEG, .PBG, .GIF, .BMP":show-file-list="false":on-success="handleAvatarSuccess":before-upload="beforeAvatarUpload":on-progress="topprogressicon"><div v-show="isopens"><i class="el-icon-loading"></i>上传中...</div><i v-show="!isopens" class="el-icon-plus avatar-uploader-icon-sf">{{uptext}}</i></el-upload></div></div>
</template><script>
import { getToken } from "@/utils/auth";
export default {props: {//改变文字uptext: {type: String,default: "上传图片",},upWith: {type: Number,default: 100,},upHeight: {type: Number,default: 100,},upBradius: {type: Number,default: 16,},upimgUrl: {type: String,},//1图片 2视频utype: {type: Number,default: 1,},},computed: {myStyle() {return {"--upWith": this.upWith + "px","--upHeight": this.upHeight + "px","--upBradius": this.upBradius + "px",};},},data() {return {updatehttp: '', //上传的接口headers: {Authorization: "Bearer " + getToken(), //自己的token},updataimg: {fileKey: "", //上传时携带参数},isopens: false, //上传加载图isok: false, //上传成功失败,dataFile: {path: "",videoTime: "",},pathUrl: "",};},watch: {upimgUrl: {handler(val) {if (val) {this.pathUrl = val;} else {this.pathUrl = "";}},deep: true,immediate: true,},},methods: {getTimes(file) {var content = file;//获取上传视频时长var url = URL.createObjectURL(content);var audioElement = new Audio(url);audioElement.addEventListener("loadedmetadata", (_event) => {this.dataFile.videoTime = parseInt(audioElement.duration);});},//上传成功handleAvatarSuccess(res, file) {if (this.isok) {this.dataFile.path = res.url;this.pathUrl = res.url;this.isopens = false;this.$emit("changeName", this.dataFile);}},//文件上传时topprogressicon(event, file, fileList) {if (this.isok) {this.isopens = true;}},//上传之前的操作beforeAvatarUpload(file) {if (this.utype == 1 && file.type.includes("image")) {const isLt2M = file.size / 1024 / 1024 < 2;if (!isLt2M) {this.$message.error("上传图片大小不能超过 2MB!");this.isok = false;} else {this.isok = true;}}else if (this.utype == 2 && file.type.includes("video")) {this.getTimes(file);const isLt2M = file.size / 1024 / 1024 < 500;if (!isLt2M) {this.$message.error("上传视频大小不能超过 500MB!");this.isok = false;} else {this.isok = true;}} else {this.$message.error("上传类型错误!");this.isok = false;}},//删除deleteimg() {this.pathUrl = "";this.$emit("changeName", "");},//预览ylimg() {this.$refs.preview.clickHandler();},},
};
</script><style scoped>
.uploads {position: relative;
}.avatar-uploader /deep/ .el-upload {width: var(--upWith);height: var(--upHeight);border-radius: var(--upBradius);
}.avatar-uploader-icon-sf {color: #999999;
}.avatar-uploader {border: 1px dashed #d9d9d9;width: 100%;text-align: center;
}.isblock {width: 100%;
}.isblock:hover .imgs_prews {display: block;
}.imgs_prews {display: none;
}.imgs_prew {position: absolute;top: 0;width: 100%;display: flex;justify-content: center;align-items: center;background: rgba(0, 0, 0, 0.6);color: #fff;
}/* 左右图标 */
.imgs_prew i {display: inline-block;font-size: 20px;cursor: pointer;
}.imgs_prew span {width: 2px;height: 18px;margin: 0px 20px;background-color: #fff;
}
</style>

组件注册两种方法。

1.全局注册直接

//上传图片
import UploadFile from '@/components/UploadImg'
Vue.component('UploadFile', UploadFile)

2.单页面使用

import UploadFile from "../../../components/UploadImg";
export default {name: "Post",components: { UploadFile },
}

页面如何使用

<template><div>//注意上传视频要修改utype为2<UploadFile:utype='1':up-with="140"  :upimgUrl="form.companyHead":up-height="160":up-bradius="10"uptext="请上传封面图"@changeName="changUrl"></UploadFile></div>
</template>methods: {changUrl(e){//e.videoTime 获取到视频的时长if (e) {this.form.companyVideo = e.path;} else {this.form.companyVideo = "";}}},

相关文章:

  • c++/c语法基础【2】
  • 灵动微单片机洗衣机方案——【软硬件开发支持】
  • 安卓手机APP开发__网络连接性支持VPN
  • MySql--SQL语言
  • Android Studio 与 Gradle 及插件版本兼容性
  • 外包干了3天,技术退步明显.......
  • java文档管理系统的设计与实现源码(springboot+vue+mysql)
  • vscode插件-06 Python
  • Postman进阶功能-Mock服务与监控
  • 【STM32项目】基于stm32智能鱼缸控制系统的设计与实现(完整工程资料源码)
  • 基于消息中间件的异步通信机制在系统解耦中的优化与实现
  • Kubeblocks系列2-redis尝试之出师未捷身先死
  • (六)DockerCompose安装与配置
  • 【MATLAB】基于EMD-PCA-LSTM的回归预测模型
  • Object类,你学会了吗?
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • 30天自制操作系统-2
  • Android开源项目规范总结
  • docker python 配置
  • ESLint简单操作
  • iBatis和MyBatis在使用ResultMap对应关系时的区别
  • Java深入 - 深入理解Java集合
  • js算法-归并排序(merge_sort)
  • MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • PHP CLI应用的调试原理
  • spring学习第二天
  • Vue 重置组件到初始状态
  • Vue.js源码(2):初探List Rendering
  • windows下使用nginx调试简介
  • 构造函数(constructor)与原型链(prototype)关系
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 深度学习中的信息论知识详解
  • 携程小程序初体验
  • 一、python与pycharm的安装
  • 因为阿里,他们成了“杭漂”
  • No resource identifier found for attribute,RxJava之zip操作符
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • 如何用纯 CSS 创作一个货车 loader
  • ​必胜客礼品卡回收多少钱,回收平台哪家好
  • ‌‌雅诗兰黛、‌‌兰蔻等美妆大品牌的营销策略是什么?
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (14)目标检测_SSD训练代码基于pytorch搭建代码
  • (145)光线追踪距离场柔和阴影
  • (4)事件处理——(2)在页面加载的时候执行任务(Performing tasks on page load)...
  • (Matlab)使用竞争神经网络实现数据聚类
  • (MTK)java文件添加简单接口并配置相应的SELinux avc 权限笔记2
  • (SpringBoot)第七章:SpringBoot日志文件
  • (八)Flask之app.route装饰器函数的参数
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (附源码)ssm高校志愿者服务系统 毕业设计 011648
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析