uniapp 一次性上传多条视频 u-upload accept=“video“ uni.chooseMedia uni.uploadFile

在这里插入图片描述

方式 一 部分安卓机 只能一条一条传视频

文档地址 uview 2.0 Upload 上传组件
html

<view class="formupload">
	<u-upload 
	accept="video"
	:fileList="fileList3" @afterRead="afterRead" @delete="deletePic" name="3" multiple
		:maxCount="9"></u-upload>
</view>

script

	// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片 afterRead
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(event.name,lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(num,url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: baseUrl + 'upload', // 上传图片视频的接口名
						filePath: url,
						name: 'file',
						header: {},
						formData: {
							file: url
						},
						success: (res) => {
							res.data = JSON.parse(res.data)
							if (res.data.code == 0) {
								setTimeout(() => {
									resolve(res.data.url)
								}, 1000)
							} else {
								uni.showToast({
									title: '上传失败',
									icon: 'none'
								});
								this[`fileList${num}`].pop()
								return
							}
						},
						fail: (error) => {
							uni.showToast({
								title: '上传失败',
								icon: 'none'
							});
							reject(error)
						}
					});
				})
			},

方式 二 可以一次性上传多条视频

文档地址 video
uni.chooseMedia
uni.uploadFile

html

	<view class="u-flex wrap">
		<image src="@/static/image/up.png" mode="" class="u-w-160 u-h-160 u-m-r-16 u-m-b-16" 
		@click="upvideoFun" v-if="fileList3.length < 9"></image>
	<view class="vibox" v-for="(item,i) in fileList3" :key="i">
		<video :src="item.url" class="viimg"></video>
		<view class="vitext" @click="delFun(i)">  <text>x</text>  </view>	
	</view>
	</view>

script

	// 上传视频
		 upvideoFun(){
				let that = this;
				uni.chooseMedia({
				  count: 9,
				  mediaType: ['video'],
				  sourceType: ['album', 'camera'],
				  maxDuration: 30,
				  camera: 'back',
				  success(res) {
						let lists = [].concat(res.tempFiles);
						for (let i = 0; i < lists.length; i++) {
								uni.uploadFile({
									url: baseUrl + 'upload',
									filePath: lists[i].tempFilePath,
									name: 'file',
									formData: {},
									success: (resres) => {
										let data = JSON.parse(resres.data);
										that.fileList3.push({
											url:data.url
										});
									}
								});
						}
				  }
				});
			},
	
			// 删除视频
			delFun(i){
				this.fileList3.splice(i, 1);
			},

css

.vibox{
		width: 80px;
		height: 80px;
		position: relative;
		border-radius: 2px;
		margin: 0 8px 8px 0;
		overflow: hidden;
		display: flex;
		flex-direction: row;
		.viimg{
			width: 80px;
			height: 80px;
		}
		.vitext{
			position: absolute;
			top: 0;
			right: 0;
			background-color: #373737;
			height: 14px;
			width: 14px;
			display: flex;
			border-bottom-left-radius: 100px;
			align-items: center;
			justify-content: center;
			z-index: 3;
			text{
				font-size: 10px;
				font-weight: normal;
				top: 0px;
				color: #ffffff;
				transform: translate(1px,-3px);
			}
		}
	}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值