Bootstrap

uniapp中预览pdf方法

1.创建一个页面,文件名随便起一个放到page中

2. 

<template>
	<view>
		<web-view :update-title="false" v-if="!showVideo" :src="pageUrl" @message="handleMessage"></web-view>
		<video v-else style="width: 100%;" :autoplay="true" :title="title" @error="playErro" :src="pageUrl"></video>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageUrl: null,
				showVideo: false,
				title: ''
			}
		},
		onLoad(option) {
			// this.pageUrl = decodeURIComponent(option.url)
			this.parseUrl(decodeURIComponent(option.url));//接受pdf的地址
			this.title = option.title;
			uni.setNavigationBarTitle({
				title: this.title
			})
			console.log(option,"optionoptionoption")
		},
		methods: {
			playErro() {
				uni.$u.toast("播放错误")
			},
			
			handleMessage(evt) {
				let data = evt.detail.data[0];
				console.log('消息传递:' + data.editor);
				this.getOpenerEventChannel().emit('resultData',data.editor);
				uni.navigateBack({});
			},
			parseUrl(url) {
				var fileEnd = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx'];
				var type = this.getEnd(url, '.');
				if (fileEnd.indexOf(type) >= 0) {
					console.log('打开文档');
					// plus.runtime.openURL('https://view.officeapps.live.com/op/embed.aspx?src='+url);
					var goUrl = 'https://view.officeapps.live.com/op/embed.aspx?src=' + url;
					if (type == 'pdf') {
						let base_url = '../../static/pdf/html/web/viewer.html';
						goUrl = base_url + '?file=' + url;
					}
					this.pageUrl = goUrl;
				} else if (type == 'mp4' || type == 'm3u8' || url.indexOf('rtmp://') > -1) {
					this.showVideo = true;
					this.pageUrl = url;
				} else {
					this.pageUrl = url;
				}

			},
			getEnd(text, str) {
				var index = text.lastIndexOf(str);
				return text.substring(index + 1, text.length);
			},
		}
	}
</script>

<style>

</style>

3.直接将url放到跳转地址后面。

 

;