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

这篇具有很好参考价值的文章主要介绍了uniapp开发小程序,上传图片和视频功能。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.需求:

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

2.效果图

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

uniapp开发小程序,上传图片和视频功能,uniapp,uni-app,小程序,音视频文章来源地址https://www.toymoban.com/news/detail-654516.html

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>

完成!

到了这里,关于uniapp开发小程序,上传图片和视频功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • uni-app开发小程序使用uni.chooseMedia选择图片,安卓手机无法选择图片

    一、在小程序中,选择图片并上传,是一个很常见的功能; 二、最近在开发中,使用 uni.chooseMedia 来做选择图片功能时,苹果手机是正常的,安卓手机打不开!!!代码如下: 就这点屁代码,苹果手机可以正常打开选择图片的功能,安卓手机没法打开!!! !!!! 我百思

    2024年02月16日
    浏览(29)
  • uni-app开发 小程序直播功能

    1、微信后台申请插件开通 2、微信后台开通直播功能 3、代码中接入直播插件AppID 4、【直播组件】如何使用 5、直播组将状态获取 微信开发直播功能,需要企业账号; 需要申请直播功能和插件 1、微信后台申请插件开通 微信后台 登录微信后台 点击设置中的第三方设置 — 添

    2024年02月05日
    浏览(28)
  • uniapp小程序实现上传图片功能,并显示上传进度

    效果图: 实现方法: 一、通过uni.chooseMedia(OBJECT)方法,拍摄或从手机相册中选择图片或视频。 官方文档链接: https://uniapp.dcloud.net.cn/api/media/video.html#choosemedia 二、使用uni.uploadFile(OBJECT)方法上传文件。 官方文档链接: https://uniapp.dcloud.net.cn/api/request/network-file.html#uploadfile 三、使

    2024年02月12日
    浏览(40)
  • uniapp,小程序上传图片,调用相机,打开相册功能

    uniapp上传头像上传图片,上传评价等功能都可以使用这个方法 将接口替换和参数替换其他共用直接使用即可,需要弹窗就使用俩个参数,直接打开相册或者相机就只填一个参数

    2024年02月14日
    浏览(30)
  • uni-app 小程序上传图片报错:uploadFile:fail parameter error: parameter.filePat…meter.name should be...

    uni-app 开发过程中,发现上传功能在小程序端报错: uploadFile:fail parameter error: parameter.filePat…meter.name should be String instead of Undefined 。 查看 uni.uploadFile(options) 文档,是在传输参数格式出现了错误。 按照官方文档配置如下即可: H5正常如下: 小程序如下: 参考了一下别人的说

    2024年02月05日
    浏览(29)
  • 微信小程序使用uni-app开发小程序及部分功能实现详解心得

    目录 一、uni-app 1、简介 2、开发工具 3、新建 uni-app项目 4、把项目运行到微信开发者工具 二、实现tabBar效果 1、创建tabBar页面 2、配置tabBar 1、创建分包目录 2、在 pages.json 文件中配置 3、创建分包页面 四、公用方法封装 五、搜索功能 1、搜索组件 2、搜索建议实现 3、本地存储

    2024年02月11日
    浏览(43)
  • 【uni-app】上传图片 uni.chooseImage(),uni.uploadFile()接口使用

    图片上传在实际场景中使用广泛,例如商品图片,汽车图片等等 实现选择单张图片上传,可以删除图片。(预览功能时间原因未研究) Vue2 版本,使用uni-app框架api唤起手机相册等图片源,将图片选中到目标列表,并发送到服务器存储,存储成功得到处理后的图片名称存储到

    2024年02月05日
    浏览(34)
  • uni-app 实现图片上传添加水印操作

    改进原因: 1、Canvas 2D(新接口)需要显式设置画布宽高,默认:300 150,最大:1365 1365 ios 无法上传较大图片的尺寸,固对超过此尺寸的图片进行了等比缩放的处理; 2、在页面中设置canvas宽高,导致页面有滚动条;现在采用离屏的canvas,但是离屏的canvas,canvasToTempFilePath方法

    2024年02月07日
    浏览(53)
  • 解决uni-app开发小程序时,CSS调用本地图片当背景时不能使用的问题

    uniapp官方给出的解释就是小程序不支持本地图片,只支持网络访问或者base64。 当背景图片小于40kb的时候还好,uniapp会自动转为base64格式;但是大于40kb时候就不行了,目前我了解的有三种方式解决: 1.可以通过动态样式“:style”来解决,在标签上如下编写: 接着在data里声明

    2024年02月12日
    浏览(30)
  • uni-app实现上传图片或者文件(直接上代码)

    在webappsRoot文件夹下创建用于接收上传文件的upload文件夹 如果是文件: 上传文件方法也可以上传图片,只需要把extension中改为图片对应为后缀名即可 controller:

    2024年02月11日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包