uniapp PDF 预览和下载

创建 index.vue

javascript 复制代码
<template>
	<view>
		
		<view class="box">
		
			<view class="item" >
				<view class="title">
					<span></span>
					<text>文件</text>
				</view>
				<view class="list" v-for="(item,index) in pdfList" :key="index">
					<view class="l">
						<view class="icon">
							<image src="@/static/PDF.png" mode=""></image>
						</view>
						<view class="name">
							{{info.assembly_file_name}}({{index+1}})
						</view>
					</view>
					<view class="btn">
						<view class="yl" @click="clickYl(item)">
							预览
						</view>
						<view class="xz" @click="clickXz(item,info.assembly_file_name)">
							下载
						</view>
					</view>
				</view>
			</view>
			
		</view>

	</view>
</template>

<script>
	import {
		infoApi
	} from "@/api/index.js"

	export default {
		data() {
			return {
				pdfList: [],
				info: {},

			}
		},

		onLoad() {
			
		},

		methods: {
			
			
			clickYl(url) {
				let _this = this
				uni.showLoading({
					title: '加载中...'
				})

				uni.downloadFile({
					//需要预览的文件地址
					url: url,
					success: (res) => {
						if (res.statusCode === 200) {
							//下载成功,得到文件临时地址
							console.log('下载成功', res.tempFilePath);
							//条件编译,若为h5端则直接赋值文件地址			
							// #ifdef H5
							let newUrl = res.tempFilePath
							// #endif

							//条件编译,若为App端,则需要将本地文件系统URL转换为平台绝对路径	
							// #ifdef APP-PLUS
							let newUrl = plus.io.convertLocalFileSystemURL(res.tempFilePath)
							// #endif
							uni.navigateTo({
								url: '/pages/index/pdf?url=' + newUrl
							})
						} else {
							uni.showToast({
								title: '预览失败',
								icon: "none"
							})
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '预览失败',
							icon: "none"
						})
					}
				})


			},
			clickXz(fileUrl, attachmentName) {
				uni.downloadFile({
					//需要预览的文件地址
					url: fileUrl,

					success: (res) => {
						if (res.statusCode === 200) {
							uni.hideLoading();
							//下载成功,得到文件临时地址
							console.log('下载成功', res.tempFilePath);
							let newUrl = res.tempFilePath
							// 创建一个临时的 <a> 元素用于下载
							const link = document.createElement('a');
							link.href = newUrl;
							link.setAttribute('download', attachmentName);
							document.body.appendChild(link);
							link.click();
							document.body.removeChild(link);
							URL.revokeObjectURL(link.href);
							uni.showToast({
								title: '下载成功',
								icon: 'none',
							})
						} else {
							uni.hideLoading();
							uni.showToast({
								title: '下载失败',
								icon: 'none',
							})
						}

					},
					fail() {
						uni.hideLoading();
						uni.showToast({
							title: '下载异常',
							icon: 'none',
						})
					}
				});


			},

		

		}
	}
</script>

<style lang="less" scoped>
	page {
		background: #F6F7FB;
	}

	.bgc {
		width: 100%;
		height: 88rpx;
		position: relative;
		background: linear-gradient(109deg, #E8F5F7, #fde1c4 100%);


	}

	.box {
		margin: 32rpx;

		.item {
			padding: 32rpx;
			margin-bottom: 48rpx;
			box-sizing: border-box;
			border-radius: 24rpx;
			background-color: #fff;

			.title {
				display: flex;
				align-items: center;
				margin-bottom: 32rpx;

				span {
					display: inline-block;
					width: 8rpx;
					height: 32rpx;
					background: #f98518;
					border-radius: 4rpx;
				}

				text {
					font-size: 28rpx;
					font-family: PingFang HK, PingFang HK-500;
					font-weight: 500;
					text-align: LEFT;
					color: #333333;
					line-height: 32rpx;
					margin-left: 16rpx;
				}
			}

			.list {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 32rpx;

				.l {
					display: flex;
					align-items: center;
					flex: 1;

					.icon {
						width: 56rpx;
						height: 68rpx;

						image {
							width: 100%;
							height: 100%;
						}
					}




					.name {
						width: 75%;
						font-size: 28rpx;
						font-family: PingFang HK, PingFang HK-400;
						font-weight: 400;
						text-align: LEFT;
						color: #333333;
						margin-left: 22rpx;
					}


				}

				.btn {
					display: flex;

					.yl,
					.xz {
						font-size: 24rpx;
						font-family: PingFang HK, PingFang HK-400;
						font-weight: 400;
						text-align: LEFT;
						color: #f98518;
					}

					.yl {
						margin-right: 32rpx;
					}
				}
			}

			.lists {

				.items {
					width: 100%;
					background: #ffffff;
					border-radius: 24rpx;
					box-sizing: border-box;
					margin-bottom: 32rpx;

					.name {
						font-size: 24rpx;
						font-family: PingFang HK, PingFang HK-500;
						font-weight: 400;
						text-align: LEFT;
						color: #333333;
						width: 100%;
						margin-bottom: 12rpx;

					}


				}
			}
		}

	}
</style>

2:创建PDF.vue

javascript 复制代码
<template>
	<view>
	
		<web-view :src="pdfUrl"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pdfUrl:'',
				from:''
			}
		},
		onLoad({url}) {
			/**
			 * 浏览情景
			 */
			// 浏览情景1:浏览本地的pdf文件
			// this.pdfUrl = '/hybrid/html/web/viewer.html?file=https://beoka-file.oss-cn-beijing.aliyuncs.com/beoka_oxy/img/1000001435995357.pdf'
			
			// 浏览情景2:浏览在线的pdf文件
			this.pdfUrl = '/static/hybrid/html/web/viewer.html?file='+encodeURIComponent(url)
		},
		
	}
</script>

<style>

</style>
相关推荐
Mintopia2 分钟前
🤖 AI 决策 + 意图OS:未来软件形态的灵魂共舞
前端·人工智能·react native
攀登的牵牛花2 分钟前
前端向架构突围系列 - 框架设计(四):依赖倒置原则(DIP)
前端·架构
程序员爱钓鱼10 分钟前
Node.js 编程实战:测试与调试 —— 日志与监控方案
前端·后端·node.js
Mapmost19 分钟前
数字孪生项目效率翻倍!AI技术实测与场景验证实录
前端
小酒星小杜23 分钟前
在AI时代,技术人应该每天都要花两小时来构建一个自身的构建系统-Input篇
前端·程序员·架构
Cache技术分享31 分钟前
290. Java Stream API - 从文本文件的行创建 Stream
前端·后端
陈_杨33 分钟前
前端成功转鸿蒙开发者真实案例,教大家如何开发鸿蒙APP--ArkTS 卡片开发完全指南
前端·harmonyos
Mr -老鬼35 分钟前
移动端跨平台适配技术框架:从发展到展望
android·ios·小程序·uni-app
小杨同学4939 分钟前
C 语言实战:枚举类型实现数字转星期(输入 1~7 对应星期几)
前端·后端
陈_杨41 分钟前
前端成功转鸿蒙开发者真实案例,教大家如何开发鸿蒙APP--ArkTS 卡片刷新机制
前端·harmonyos