uniapp开发小程序,上传图片和视频功能

2023-11-03

1.需求:

可以上传图片和视频,并且都可以删除,图片可以预览。

2.效果图

在这里插入图片描述

在这里插入图片描述

3.代码:

 <template>
 	<!-- 上传start -->
 	<view style="display: flex; flex-wrap: wrap;">
 		<view class="update-file">
 			<!--图片-->
 			<view v-for="(item,index) in imageList" :key="index">
 				<view class="upload-box">
 					<image class="preview-file" :src="hostUrl+item" @tap="previewImage(item)"></image>
 					<view class="remove-icon" @tap="delect(index)">
 						X
 						<!-- <image src="../../static/images/del.png" class="del-icon" mode=""></image> -->
 					</view>
 				</view>
 			</view>
 			
 			<!--视频-->
 			<view v-for="(item1, index1) in srcVideo" :key="index1">
 				<view class="upload-box">
 					<video class="preview-file" :src="item1"></video>
 					<view class="remove-icon" @tap="delectVideo(index1)">
						X
 						<!-- <image src="../../static/images/del.png" class="del-icon" mode=""></image> -->
 					</view>
 				</view>
 			</view>
 			
			<!--按钮-->
 			<view v-if="VideoOfImagesShow" @tap="chooseVideoImage" class="upload-btn">
 				<!-- <image src="../../static/images/jia.png" style="width:30rpx;height:30rpx;" mode=""></image> -->
 				<view class="btn-text">上传</view>
 			</view>
 		</view>
 	</view>
 	<!-- 上传 end -->
 </template>
 <script>
 	var sourceType = [
 		['camera'],
 		['album'],
 		['camera', 'album']
 	];
 	export default {
 		data() {
 			return {
 				hostUrl: this.$api.hostImages,
 				// 上传图片视频
 				VideoOfImagesShow: true, // 页面图片或视频数量超出后,拍照按钮隐藏
 				imageList: [], //存放图片的地址
 				srcVideo: [], //视频存放的地址
 				sourceType: ['拍摄', '相册', '拍摄或相册'],
 				sourceTypeIndex: 2,
 				cameraList: [{
 					value: 'back',
 					name: '后置摄像头',
 					checked: 'true'
 				}, {
 					value: 'front',
 					name: '前置摄像头'
 				}],
 				cameraIndex: 0, //上传视频时的数量
 				//上传图片和视频
 				uploadFiles: [],
 			}
 		},
 		onUnload() {
 			// 上传
 			this.imageList = [];
 			this.sourceTypeIndex = 2;
 			this.sourceType = ['拍摄', '相册', '拍摄或相册'];
 		},
 		methods: {
 			//点击上传图片或视频
 			chooseVideoImage() {
 				uni.showActionSheet({
 					title: '选择上传类型',
 					itemList: ['图片', '视频'],
 					success: res => {
 						console.log(res);
 						if (res.tapIndex == 0) {
 							this.chooseImages();
 						} else {
 							this.chooseVideo();
 						}
 					}
 				});
 			},
 			//上传图片
 			chooseImages() {
 				uni.chooseImage({
 					count: 9, //默认是9张
 					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
 					sourceType: ['album', 'camera'], //从相册选择
 					success: res => {
 						console.log(res, 'ddddsss')
 						let imgFile = res.tempFilePaths;

 						imgFile.forEach(item => {
 							uni.uploadFile({
 								url: this.$api.host + this.$url.upload, //仅为示例,非真实的接口地址
 								method: "POST",
 								header: {
 									token: uni.getStorageSync('localtoken')
 								},
 								filePath: item,
 								name: 'file',
 								success: (result) => {
 									let res = JSON.parse(result.data)
 									console.log('打印res:', res)
 									if (res.code == 200) {
 										this.imageList = this.imageList.concat(res
 											.initialPreview[0]);
 										console.log(this.imageList, '上传图片成功')

 										if (this.imageList.length >= 9) {
 											this.VideoOfImagesShow = false;
 										} else {
 											this.VideoOfImagesShow = true;
 										}
 									} else {
 										uni.showToast({
 											title: res.msg,
 											icon: 'none'
 										})
 									}
 								}
 							})
 						})

 					}
 				})
 			},
 			
 			//上传视频
 			chooseVideo(index) {
 				uni.chooseVideo({
 					maxDuration: 60, //拍摄视频最长拍摄时间,单位秒。最长支持 60 秒
 					count: 9,
 					camera: this.cameraList[this.cameraIndex].value, //'front'、'back',默认'back'
 					sourceType: sourceType[this.sourceTypeIndex],
 					success: res => {
 						let videoFile = res.tempFilePath;

 						uni.showLoading({
 							title: '上传中...'
 						});
 						uni.uploadFile({
 							url: this.$api.host + this.$url.uploadfile, //上传文件接口地址
 							method: "POST",
 							header: {
 								token: uni.getStorageSync('localtoken')
 							},
 							filePath: videoFile,
 							name: 'file',
 							success: (result) => {
 								uni.hideLoading();
 								let res = JSON.parse(result.data)
 								if (res.code == 200) {
 									this.srcVideo = this.srcVideo.concat(res.data[0].data);
 									if (this.srcVideo.length == 9) {
 										this.VideoOfImagesShow = false;
 									}
 								} else {
 									uni.showToast({
 										title: res.msg,
 										icon: 'none'
 									})
 								}
 							},
 							fail: (error) => {
 								uni.hideLoading();
 								uni.showToast({
 									title: error,
 									icon: 'none'
 								})
 							}
 						})
 					},
 					fail: (error) => {
 						uni.hideLoading();
 						uni.showToast({
 							title: error,
 							icon: 'none'
 						})
 					}
 				})
 			},

 			//预览图片
 			previewImage: function(item) {
 				console.log('预览图片', item)
 				uni.previewImage({
 					current: item,
 					urls: this.imageList
 				});
 			},

 			// 删除图片
 			delect(index) {
 				uni.showModal({
 					title: '提示',
 					content: '是否要删除该图片',
 					success: res => {
 						if (res.confirm) {
 							this.imageList.splice(index, 1);
 						}
 						if (this.imageList.length == 4) {
 							this.VideoOfImagesShow = false;
 						} else {
 							this.VideoOfImagesShow = true;
 						}
 					}
 				});
 			},
 			// 删除视频
 			delectVideo(index) {
 				uni.showModal({
 					title: '提示',
 					content: '是否要删除此视频',
 					success: res => {
 						if (res.confirm) {
 							this.srcVideo.splice(index, 1);
 						}
 						if (this.srcVideo.length == 4) {
 							this.VideoOfImagesShow = false;
 						} else {
 							this.VideoOfImagesShow = true;
 						}
 					}
 				});
 			},
 			// 上传 end
 		}
 	}
 </script>

 <style scoped lang="scss">
 	// 上传
 	.update-file {
 		margin-left: 10rpx;
 		height: auto;
 		display: flex;
 		justify-content: space-between;
 		flex-wrap: wrap;
 		margin-bottom: 5rpx;

 		.del-icon {
 			width: 44rpx;
 			height: 44rpx;
 			position: absolute;
 			right: 10rpx;
 			top: 12rpx;
 		}

 		.btn-text {
 			color: #606266;
 		}

 		.preview-file {
 			width: 200rpx;
 			height: 200rpx;
 			border: 1px solid #e0e0e0;
 			border-radius: 10rpx;
 		}

 		.upload-box {
 			position: relative;
 			width: 200rpx;
 			height: 200rpx;
 			margin: 0 10rpx 20rpx 0;

 		}

 		.remove-icon {
 			position: absolute;
 			right: -10rpx;
 			top: -10rpx;
 			z-index: 1000;
 			width: 30rpx;
 			height: 30rpx;
 		}

 		.upload-btn {
 			width: 200rpx;
 			height: 200rpx;
 			border-radius: 10rpx;
 			background-color: #f4f5f6;
 			display: flex;
 			justify-content: center;
 			align-items: center;
 		}
 	}
 	.guide-view {
 		margin-top: 30rpx;
 		display: flex;

 		.guide-text {
 			display: flex;
 			flex-direction: column;
 			justify-content: space-between;
 			padding-left: 20rpx;

 			.guide-text-price {
 				padding-bottom: 10rpx;
 				color: #ff0000;
 				font-weight: bold;
 			}
 		}
 	}
 	.service-process {
 		background-color: #ffffff;
 		padding: 20rpx;
 		padding-top: 30rpx;
 		margin-top: 30rpx;
 		border-radius: 10rpx;
 		padding-bottom: 30rpx;

 		.title {
 			text-align: center;
 			margin-bottom: 20rpx;
 		}
 	}
 	.form-view-parent {
 		border-radius: 20rpx;
 		background-color: #FFFFFF;
 		padding: 0rpx 20rpx;

 		.form-view {
 			background-color: #FFFFFF;
 			margin-top: 20rpx;
 		}

 		.form-view-textarea {
 			margin-top: 20rpx;
 			padding: 20rpx 0rpx;

 			.upload-hint {
 				margin-top: 10rpx;
 				margin-bottom: 10rpx;
 			}
 		}
 	}


 	.bottom-class {
 		margin-bottom: 60rpx;
 	}

 	.bottom-btn-class {
 		padding-bottom: 1%;

 		.bottom-hint {
 			display: flex;
 			justify-content: center;
 			padding-bottom: 20rpx;

 		}
 	}
 </style>

完成!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

uniapp开发小程序,上传图片和视频功能 的相关文章

  • 判断 Dictionary 的值为不为空 或 undefined

    score ScoreDetails ChineseScore 130 MatchScore 180 EnglishScore 117 GeneralScore undefined WL A DL A if score ScoreDetai
  • 服务器日常初始化的脚本

    版权声明 可以任意转载 转载时请标明文章原始出处 xjtushilei和作者信息 石磊 升级了架构 采用了腾讯云的vpc 成都服务器 连续配置了12台服务器 肯定要节省点时间 所以就这样记录一下吧 之后需要的话 写成脚本更佳 省事的主要就是
  • IDEA中gitee提交代码很慢,很容易卡住不动

    看下图提交页面画红框的两项是否勾选上了 若勾选上了 取消勾选即可 你会感受到飞一般的速度
  • Kafka学习之路

    转自 https www cnblogs com huxi2b p 6308999 html 一直在思考写一些什么东西作为2017年开篇博客 突然看到一篇 Kafka学习之路 的博文 觉得十分应景 于是决定搬来这 他山之石 虽然对于Kafk
  • C++学习(三九六)如何查看.o文件

    objdump h xxxx o 打印主要段的信息 objdump x xxxx o 打印更多的详细信息 objdump s xxx o 将所有段的内容以16进制方式打印出来 objdump d xxx o 或者 S 将所有包含指令的段反汇
  • 关于流操作的知识点【Node.js】

    目录 1 流是什么 2 Node js 为什么要用流 传电影的例子 3 node js 是如何来使用流的呢
  • hdmi怎么支持2k分辨率_简单选择题!花3K买虚荣,还是2K买个踏实

    6 180越来越近了 各大电商的优惠活动从6 1就已经悄然拉开战幕 微星电竞显示器在今年6 18时段 推出了巨幅的优惠活动 各价位和各类配置的电竞显示器都给出了超值的价格 同时多款新品IPS显示器均加入了6 18暑促活动 一直以高性价比闻名
  • QT之UI界面设计与窗口显示不一致

    问题 已经在UI界面中利用布局设计好界面 但是运行程序后显示的界面出现错乱 而且在做出一些修改后重新构建 运行时界面无变化 目录 一 解决UI设计界面与运行时显示界面不一致的问题 1 导致该现象的原因有 显示屏分辨率过高 2 使用布局和设置

随机推荐

  • 干货分享丨精心整理了份Python知识点高清速查表!太受用了!

    去年底北大保安 神仙打架 的新闻 让本来就火得一塌糊涂的Python又上了把热搜 资料来源 北大官微 不过 最吸引我的不是这条微博本身 而是一条 学会Python 可以上天 的评论 此语一出 立刻遭到群嘲 最扎眼的 莫过于那句 学会Pyth
  • Hive源码阅读--导读

    总述 Hive的执行流程大致分为两部分 即任务的提交与返回 命令的编译与执行 前者在CliDriver类中流转 后者主要在Driver与ParseDriver类 核心编译在BaseSemanticAnalyzer和QueryPlan类中 任
  • C 函数 strstr 的高效实现

    C函数库中有一个函数 strstr char char 它实现的是在一个原字符串中查找一个子串 如果找到这样的一个子串 返回这个子串在原字符串中的起始位置 若没有找到这样的一个子串 则返回NULL 但是 函数库中实现的仅是一般情况下的查找
  • ValueError: not enough values to unpack (expected 3, got 2) 解决办法

    这个错误通常是由于函数返回值的数量与解包变量的数量不匹配导致的 导致这个问题的原因可能是版本不同 或函数参数的不同导致的 比如在比较新的opencv中 cv2 findContours 返回的是两个参数 而老一点的版本是三个参数 列如 bi
  • docker之volumes

    数据卷 卷是保存由 Docker 容器生成和使用的数据的首选机制 虽然绑定挂载依赖于主机的目录结构和操作系统 但卷完全由 Docker 管理 与绑定挂载相比 卷有几个优点 卷比绑定挂载更容易备份或迁移 您可以使用 Docker CLI 命令
  • CV算法工程师面试问题总结(下) 2021.06.16

    本篇主要包含数据类问题 正则化 激活函数与梯度以及回归 SVM支持向量机 K Means均值以及机器学习相关常考内容等相关面试经验 数据类问题 1 样本不平衡的处理方法 欠采样 随机删除观测数量足够多的类 使得两个类别间的相对比例是显著的
  • 【pip】解决ERROR: Could not build wheels for pycuda which use PEP 517 and cannot be installed directly

    参考 https stackoverflow com questions 64038673 could not build wheels for which use pep 517 and cannot be installed direc
  • java中四种操作(dom、sax、jdom、dom4j)xml方法

    java中四种操作 dom sax jdom dom4j xml方式详解与比较 1 DOM JAXP Crimson解析器 DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准 DOM是以层次结构组织的节点或信息片断的集合 这个层
  • csv反序列化_1.6.2python 文件复制、CSV、序列化和反序列化

    1 文件复制 单个文件复制 多个文件复制 使用系统模块 os 获取指定文件夹的所有文件名 复制流程 根据地址读取源文件 将读取的写入新地址 地址用os模块获取的文件名和文件夹名整合而成 2 CSV文件的写入与读取 导入CSV模块 CSV文件
  • Qt 使用QMediaPlayer类在VS中播放音乐

    qt有许多类都可以进行播放音频文件 这里我主要讲QMediaPlayer类 如何在vs中进行播放音乐 所遇到的问题该如何解决 QMediaPlayer可以对各种后缀的音频文件进行播放 包括 wav mp3等 1 向 pro文件中添加代码 由
  • requirejs之demo

    具体的理论就不讲了 可以参考 http www ruanyifeng com blog 2012 10 javascript module html http www ruanyifeng com blog 2012 10 asynchro
  • Linux下c++遍历文件夹中文件及读取绝对路径

    文件读取等操作是程序编写的基础 因此在总结了网上多个博客的基础上 写出了如下读取文件及保存绝对路径的代码片段 整理出来供大家学习 注意 这里dirent h是只有在Linux下才有的 include
  • c高级 day2

    1 写一个1 sh脚本 将以下内容放到脚本中 在家目录下创建目录文件 dir 在dir下创建dir1和dir2 把当前目录下的所有文件拷贝到dir1中 把当前目录下的所有脚本文件拷贝到dir2中 把dir2打包并压缩为dir2 tar xz
  • ios小程序上传文件使用onHeadersReceived获取header中的参数

    在上周做小程序上传的时候出现的问题 由于使用的oss 在安卓手机上获取header中的Etag是可以正常获取的 到了ios上传获取不到header中的参数 尝试了很多方法 后来发现onHeadersReceived可以获取到header就去
  • vi中不区分大小写查找的两种方法

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 在 vim中 进行关键字查找 如果内容中分了大小写的 那么 查找默认是区分了大小写的 比如 ssh的配置文件中 etc ssh sshd config中 要去禁用 root
  • sqlserver存储过程加密和解密

    加密存储过程 IF EXISTS SELECT name FROM sysobjects WHERE name encrypt this AND type P DROP PROCEDURE encrypt this GO USE pubs
  • Python 在 JMeter 中如何使用?

    要在JMeter中使用Python 需要使用JSR223 Sampler元素来执行Python脚本 使用JSR223 Sampler执行Python脚本时 需要确保已在JMeter中配置了Python解释器 并设置了正确的环境路径 1 确保
  • 性能测试-JMeter分布式测试及其详细步骤

    性能测试概要 性能测试是软件测试中的一种 它可以衡量系统的稳定性 扩展性 可靠性 速度和资源使用 它可以发现性能瓶颈 确保能满足业务需求 很多系统都需要做性能测试 如Web应用 数据库和操作系统等 性能测试种类非常多 有些概念也很相近 Lo
  • 如何编写一个完整的Linux命令

    作者 gzshun 原创作品 转载请标明出处 来源 http blog csdn net gzshun 一个完整的Linux命令需要有以下几个重要的部分组成 1 使用方法 2 命令行参数 3 移植性 1 使用方法 在每个命令当中 都需要提供
  • uniapp开发小程序,上传图片和视频功能

    1 需求 可以上传图片和视频 并且都可以删除 图片可以预览 2 效果图 3 代码