结合el-upload修改支持上传图片、视频并预览

结合element plus的el-upload标签,实现上传图片和视频,并支持在线预览和放大

1、html部分

htlm 复制代码
<el-form-item label="活动照片、视频">
	<el-upload
		v-model:file-list="state.photoList"
		:action="state.uploadUrl"
		accept=".jpg,.png,.jpeg,.mp4,.mov,.avi"
		list-type="picture-card"
		:limit="10"
		:on-success="handleUpload"
		:class="state.photoList.length === 10 ? 'hideUpload' : ''">
		<el-icon><Plus /></el-icon>
		<template #file="{ file }">
			<div>
				<img
					v-if="file.name.indexOf('.jpg') > -1
						|| file.name.indexOf('.png') > -1
						|| file.name.indexOf('.jpeg') > -1"
					:src="file.url"
					alt=""
					class="el-upload-list__item-thumbnail" />
				<video
					v-else-if="file.name.indexOf('.mp4') > -1
						|| file.name.indexOf('.mov') > -1
						|| file.name.indexOf('.avi') > -1"
					class="el-upload-list__item-thumbnail"
					style="width: 100%;height: 100%;"
					autoplay
					:src="file.url">
					<source :src="file.url" type="video/mp4" />
					<source :src="file.url" type="video/mov" />
					<source :src="file.url" type="video/avi" />
				</video>
				<span class="el-upload-list__item-actions">
					<span class="el-upload-list__item-preview" @click="handlePreview(file)">
						<el-icon><zoom-in /></el-icon>
					</span>
					<span
						v-if="!state.disabledBtn"
						class="el-upload-list__item-delete"
						@click="handleRemove(file)">
						<el-icon><Delete /></el-icon>
					</span>
				</span>
			</div>
		</template>
	</el-upload>
	<el-dialog v-model="state.dialogVisible">
		<img
			w-full
			:src="state.dialogImageFile.url"
			alt=""
			v-if="state.dialogImageFile.name.indexOf('.jpg') > -1
				|| state.dialogImageFile.name.indexOf('.png') > -1
				|| state.dialogImageFile.name.indexOf('.jpeg') > -1" />
		<video
			v-else-if="state.dialogImageFile.name.indexOf('.mp4') > -1
				|| state.dialogImageFile.name.indexOf('.mov') > -1
				|| state.dialogImageFile.name.indexOf('.avi') > -1"
			w-full
			style="width: 100%;height: 100%;"
			controls
			autoplay
			:src="state.dialogImageFile.url">
			<source :src="state.dialogImageFile.url" type="video/mp4" />
			<source :src="state.dialogImageFile.url" type="video/mov" />
			<source :src="state.dialogImageFile.url" type="video/avi" />
		</video>
	</el-dialog>
</el-form-item>

2、js部分

js 复制代码
const state = reactive({
	photoList: [] as any,           // 上传图片
	dialogVisible: false as boolean,
	dialogImageFile: '' as any,
	disabledBtn: false as boolean,
});

// 预览图片和视频
const handlePreview = (file: any) => {
	state.dialogImageFile = file;
	state.dialogVisible = true;
}
// 删除图片视频
const handleRemove = (file: any) => {
	state.photoList.map((item: any, index: number) => {
		if(item.response && item.response.data) {
			if(item.response.data == file.response?.data || item.response.data == file.url) {
				state.photoList.splice(index, 1);
			}
		} else if(item.url) {
			if(item.url == file.response?.data || item.url == file.url) {
				state.photoList.splice(index, 1);
			}
		}
	})
}
// 上传图片
const handleUpload = (res: any) => {
	if(res.code != 0) {
		state.photoList.pop()
		ElMessage.error(res.msg)
	}
}

3、css部分

css 复制代码
<style lang="scss" scoped>
    :deep(.hideUpload .el-upload--picture-card)  {
        display: none;
    }
    :deep(.el-upload--picture-card) {
        width: 243px;
        height: 180px;
    }
    :deep(.el-upload-list--picture-card .el-upload-list__item) {
        width: 243px;
        height: 180px;
    }
</style>
相关推荐
Xiaouuuuua8 分钟前
一个简单的脚本,让pdf开启夜间模式
java·前端·pdf
@Dream_Chaser29 分钟前
uniapp ruoyi-app 中使用checkbox 无法选中问题
前端·javascript·uni-app
深耕AI31 分钟前
【教程】在ubuntu安装Edge浏览器
前端·edge
倔强青铜三35 分钟前
苦练Python第4天:Python变量与数据类型入门
前端·后端·python
倔强青铜三44 分钟前
苦练Python第3天:Hello, World! + input()
前端·后端·python
上单带刀不带妹1 小时前
JavaScript中的Request详解:掌握Fetch API与XMLHttpRequest
开发语言·前端·javascript·ecmascript
倔强青铜三1 小时前
苦练Python第2天:安装 Python 与设置环境
前端·后端·python
我是若尘1 小时前
Webpack 入门到实战 - 复习强化版
前端
晓13131 小时前
JavaScript基础篇——第五章 对象(最终篇)
开发语言·前端·javascript
倔强青铜三1 小时前
苦练Python第1天:为何要在2025年学习Python
前端·后端·python