uniapp 视频上传与展示

24 篇文章 0 订阅

项目场景:

uniapp 上传视频阿里OSS对象存储


需求描述:

H5移动端开发中需要用户上传2秒左右视频的需求时。需要将视频上传至阿里oss中存储,并且下载到当前页面进行展示。

解决方案:

需要用到的依赖包:

npm install ali-oss --save 

创建一个 upload.js 文件:

const OSS = require("ali-oss"); //引入安装包;

//配置 阿里 oss 存储对应的 bucket 桶
var client = new OSS({
	region: "oss-cn-beijing", //指申请OSS服务时的地域
	endpoint: "https://oss-cn-beijing.aliyuncs.com", //指定为HTTPS,也可以是IP的形式 region将会被忽略
	accessKeyId: "xxxxxxxxxxxxxxxxxx", //  填写你的accessKeyId
	accessKeySecret: "xxxxxxxxxxxxxxxxxxxxx", // 填写你的accessKeySecret
	bucket: "xxxx", // 桶的名称
	secure: true // 配合region使用。如果指定secure为true,则使用HTTPS访问。
});

// e : 文件对象,filename:存储文件名
export function uploadVid(e, filename) {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '正在上传...'
		});
		if (e.tempFile.length == 0) {
			uni.hideLoading();
			uni.showToast({
				title: '未找到视频',
				icon: 'none'
			})
			return false;
		};
		let file = e.tempFile;
		if (file.size > 20971520) { // 20M  
			uni.showToast({
				title: "您上传的视频文件过大,请重新上传1-2秒的人脸视频!",
				icon: "none"
			});
			return false
		}
		let time = new Date(),
			y,
			m,
			d;
		y = time.getFullYear();
		m = String(time.getMonth() + 1).length === 2 ?
			time.getMonth() + 1 :
			"0" + (time.getMonth() + 1);
		d = tring(time.getDate()).length === 2 ?
			time.getDate() :
			"0" + time.getDate();
		let dfilefold = y + m + d;
		let r = Math.random().toString(16).substr(2);
		let name = filename + dfilefold + "/" + r + ".mp4";
		client.multipartUpload(name, file).then(function(result) {
				uni.hideLoading();
				uni.showToast({
					title: "上传成功,请点击提交",
					icon: "none"
				})
				let url = result.res.requestUrls[0];
				var index = url.indexOf("?");
				url = url.substring(0, index);
				resolve(url);
			})
			.catch((err) => {
				uni.hideLoading();
				uni.showToast({
					title: "上传失败",
					icon: "none"
				})
				reject(err)
			});
	}).catch(err => {
		reject(err)
	})
}

在uniapp 项目组件中

<template>
	<view class="">
		<view class="">
					视频认证(上传1-2秒左右人脸视频):
		</view>
		<view class="">
			<video style="height: 310rpx;" :src="personVideo" controls />
			<button @tap="uploadVidfn('realname/')">上传视频</button>
		</view>
	</view>
</template>

<script>
import {uploadVid} from "../utils/upload.js";
	export default {
		data() {
			return {
				personVideo: "",
			};
		},
	methods:{
		// 选择视频上传
		uploadVidfn(filename) {
			let that = this;
				uni.chooseVideo({
					success: (res) => {
						uploadVid(res, filename).then(res => {
						that.personVideo = res;
					});
				}
			})
	  	 },
	   }
</script>

点赞 评论 收藏 ~~ 有疑惑的小伙伴,可能是我表达不清楚,可以留言讨论,如有错误,也希望大家不吝指出。 ~~ 点赞 评论 收藏
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
UniApp中实现视频上传可以使用uni.uploadFile方法。以下是一个简单的示例: ```javascript // 在页面中引入uni-upload组件 import uniUpload from '@/components/uni-upload/uni-upload.vue' export default { components: { uniUpload }, data() { return { videoFile: null, videoUrl: '' } }, methods: { handleVideoChange(e) { this.videoFile = e.target.files[0] }, uploadVideo() { uni.uploadFile({ url: 'your_upload_url', // 上传接口地址 filePath: this.videoFile.path, // 视频文件路径 name: 'file', // 上传文件的字段名 success: (res) => { // 上传成功后的处理逻辑 console.log(res.data) // 可以将返回的视频地址保存到videoUrl中,用于展示或其他操作 this.videoUrl = res.data }, fail: (err) => { // 上传失败后的处理逻辑 console.log(err) } }) } } } ``` 在模板中,你可以使用`uni-upload`组件来呈现上传按钮,并监听change事件来获取用户选择的视频文件: ```html <template> <view> <uni-upload @change="handleVideoChange"></uni-upload> <button @click="uploadVideo">上传视频</button> <video :src="videoUrl" controls></video> </view> </template> ``` 上述代码中,`handleVideoChange`方法会在用户选择视频文件时被调用,将选择的文件赋值给`videoFile`。`uploadVideo`方法会在点击上传按钮时触发,通过`uni.uploadFile`方法将视频文件上传到指定的接口地址。上传成功后,可以将返回的视频地址保存到`videoUrl`中,并在页面中展示。 请注意,以上代码只是一个简单的示例,实际项目中你可能还需要对文件类型、大小进行校验,以及添加进度条等更完善的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shaoin_2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值