uniapp预览文件(pdf、视频、音频、图片、xls、docx)效果demo(整理)

在这里插入图片描述
注意:只能真机调试预览

<template>
	<view class="demo">
		<!-- <status-bar title="测试"></status-bar> -->
		<view class="activity_enclosure margin-top100" v-if="attachmentTemplateList.length > 0">
			<view class="top_box">
				<view class="top_box_circle">
					<view class="min_circle_1"></view>
					<view class="min_circle_2"></view>
				</view>
				<view class="top_box_text">附件:{
   
   {schedule}}%</view>
			</view>
			<view class="bottom_box">
				<view class="enclosure_box" v-if="attachmentTemplateList.length > 0">
					<view class="enclosure_item" v-for="(item, index) in attachmentTemplateList" :key="index">
						<text>{
   
   { index + 1 }}、</text>
						<text>{
   
   { item.title }}</text>
						<text class="" v-if="item.id">(必须填写并上传)</text>
						<text class="">:</text>
						<view class="preview_box" @click="previewFile1(item)">点击预览</view>
						<!-- <view class="preview_box" v-else @click="previewFile(item)">不可预览</view> -->
					</view>
					<!-- item.template 这是预览的文件 -->
				</view>
				<view v-else class="no_enclosure_box">暂无信息</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
      
      
		components: {
      
      

		},
		data() {
      
      
			return {
      
      
				attachmentTemplateList: [{
      
      
					title: 'pdf',
					id: 1,
					pdf:'https://images.lyyouyue.com/8793216826490707576391.pdf',
					template: 'https://curriculum-image.yizhiweixin.com/yizhiClassroom/saveLesson/material/20230310152419_RpDnjKaaa.pdf',
				}, {
      
      
					title: 'mp4视频',
					id: 2,
					template: 'https://system.lyyouyue.com/video/16822330115334982.mp4',
				}, {
      
      
					title: 'mp3音频',
					id: 20,
					template: 'https://downsc.chinaz.net/Files/DownLoad/sound1/202205/y819.mp3',
				}, {
      
      
					title: '图片',
					id: 3,
					template: 'http://img.touxiangwu.com/2020/3/uq6Bja.jpg',
				}, {
      
      
					title: 'rar压缩包-这个预览不行',
					id: 4,
					template: 'https://downsc.chinaz.net/Files/DownLoad/website/mojocosmetics.us.rar',
				}, {
      
      
					title: 'xls文件',
					id: 5,
					template: 'https://images.lyyouyue.com/8287616822323292992223.xls',
				}, {
      
      
					title: 'docx文件',
					id: 6,
					template: 'https://images.lyyouyue.com/8551716822325882022384.docx',
				}],
				schedule: 0, //下载进度1-100%
			}
			// 
		},
		// 侦听器
		watch: {
      
      

		},
		// 计算属性
		computed: {
      
      

		},
		//组件创建
		created() {
      
      

		},
		// 页面加载
		onLoad(e) {
      
      

		},
		// 页面显示
		onShow() {
      
      

		},
		// 方法
		methods: {
      
      
			// 预览图片和文件成功
			previewFile1(item) {
      
      
				// 流程步骤: 利用下载文件功能 先生成临时文件 - 调用api打开临时文件里面的内容,从而生成预览的效果,但是这个未能真实的下载到本地
				// 1.pdfUrl  可以直接在浏览器打开  2.一定要在小程序后台配置该pdf的域名 让其可以下载
				uni.showLoading({
      
      
					title: '正在打开...'
				});
				this.schedule = 0; //载入进度为0

				const downloadTask = uni.downloadFile({
      
      
					url: item.template, // 图片或者文件地址
					success: function(res) {
      
      
						console.log('下载的res', res);
						var filePath = res.tempFilePath; // 临时文件存储路径
						//  文件打开文件预览
						uni.openDocument({
      
      
							filePath: encodeURI(filePath),
							// fileType: 'pdf',
							success: function(res) {
      
      
								uni.hideLoading();
								console.log('打开文档成功');
							},
							fail: function(err) {
      
      
								uni.hideLoading();
								uni.showToast({
      
      
									title: '打开失败',
									duration: 1500,
									icon: 'none'
								});
								console.log('打开失败');
							}
						});
					},
					fail: function(err) {
      
      
						console.log('下载失败原因', err);
						uni.hideLoading();
						uni.showModal({
      
      
							title: '您需要授权相册权限',
							success(res) {
      
      
								if (res.confirm) {
      
      
									uni.openSetting({
      
      
										success(res) {
      
      },
										fail(res) {
      
      
											console.log(res);
										}
									});
								}
							}
						});
					}
				});
				downloadTask.onProgressUpdate(res => {
      
      
					console.log('下载进度' + res.progress);
					this.schedule = res.progress
					// console.log('已经下载的数据长度' + res.totalBytesWritten);
					// console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
				});
				// console.log(item, this.$url(item.template), '加载的当前文件');
				// this.$apiGet(`/api/file/download/${item.template}`).then(res => {
      
      
				// 	console.log(res, '下载的文件');
				// });
			},
		},
		// 页面隐藏
		onHide() {
      
      

		},
		// 页面卸载
		onUnload() {
      
      

		},
		// 触发下拉刷新
		onPullDownRefresh() {
      
      

		},
		// 页面上拉触底事件的处理函数
		onReachBottom() {
      
      

		},
	}
</script>

<style lang="scss" scoped>
	.demo {
      
      
		height: 100vh;
		overflow: auto;
		background-color: #f5f5f5;
		color: #FFFFFF;

		// 附件
		.activity_enclosure {
      
      
			margin-top: 60rpx;

			.top_box {
      
      
				display: flex;
				justify-content: flex-start;
				margin-bottom: 18rpx;

				.top_box_circle {
      
      
					margin-right: 22rpx;
					width: 32rpx;
					display: flex;
					justify-content: flex-start;
					align-items: center;

					.min_circle_1 {
      
      
						width: 16rpx;
						height: 16rpx;
						background: rgba(218, 0, 23, 0.7);
						border-radius: 50%;
						margin-right: -6rpx;
					}

					.min_circle_2 {
      
      
						width: 16rpx;
						height: 16rpx;
						background: rgba(1, 68, 151, 0.7);
						border-radius: 50%;
					}
				}

				.top_box_text {
      
      
					font-size: 36rpx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 600;
					color: #333333;
				}
			}

			.bottom_box {
      
      
				.enclosure_box {
      
      
					.enclosure_item {
      
      
						margin-bottom: 50rpx;
						color: #999999;

						.preview_box {
      
      
							display: inline-block;
							width: 140rpx;
							height: 40rpx;
							padding: 0rpx 14rpx;
							background: #e5ecf4;
							color: #014497;
							border-radius: 8rpx;
							margin-left: 12rpx;
						}
					}
				}

				.no_enclosure_box {
      
      
					width: 690rpx;
					// height: 48rpx;
					// line-height: 48rpx;
					font-size: 28rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					text-align: center;
				}
			}
		}
	}
</style>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38881495/article/details/130322639