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>
相关推荐
两个西柚呀1 小时前
未在props中声明的属性
前端·javascript·vue.js
子伟-H53 小时前
App开发框架调研对比
前端
桃子不吃李子3 小时前
axios的二次封装
前端·学习·axios
2501_916008894 小时前
用多工具组合把 iOS 混淆做成可复用的工程能力(iOS混淆|IPA加固|无源码混淆|Ipa Guard|Swift Shield)
android·开发语言·ios·小程序·uni-app·iphone·swift
SteveJrong4 小时前
面试题 - JavaScript
前端·javascript·面试·ecmascript·基础·找工作·红宝书
阿金要当大魔王~~4 小时前
uniapp 页面标签 传值 ————— uniapp 定义 接口
前端·javascript·uni-app·1024程序员节
全栈软件开发4 小时前
uniapp三端影视源码苹果cms自动采集电影视频网站源码前端源码带VIP
前端·uni-app·影视源码
chxii4 小时前
10.4FormData :前端文件上传与表单数据处理的核心工具
前端
AntBlack5 小时前
不当韭菜 : 好像真有点效果 ,想藏起来自己用了
前端·后端·python
楊无好5 小时前
react中props的使用
前端·react.js·前端框架