uniApp移动端安卓中使用webview打开pdf文件是下载而不是预览解决方案

关键 使用到 pdf.js

第一步:

下载pdf.js 文件到项目根目录

也就是这个文件

附下载地址:uni-app-pdf: 在uni-app中使用pdf.js实现在手机上打开pdf 

 也可通过其他方法下载 如npm 

第二步:

拷贝hybrid文件到项目根目录

第三步:

新建 viewPdf.vue文件

<template>
		<view>
			<web-view :src="webViewSrc"></web-view>
		</view>
</template>

<script setup lang="ts">
	import { ref, getCurrentInstance } from 'vue';
	import config from "@/utils/http/config.js";
	import { onLoad } from '@dcloudio/uni-app';
	const { proxy } : any = getCurrentInstance()
	let Url = ref()
	Url.value = config.filesUrl.dev

	let webViewSrc = ref()

	let viewerUrl = '/hybrid/html/web/viewer.html'; // 根目录文件地址

	onLoad((option : any) => {
		 // option.url 从其他需要预览pdf文件的页面传入的url
		let deviceInfo = uni.getDeviceInfo()
		if (deviceInfo.platform !== 'ios') {
			//option.url  就是预览的pdf地址
			webViewSrc.value = `${viewerUrl}?file=${Url.value + option.url}`
		} else {
			// ios,直接访问pdf所在路径
			webViewSrc.value = Url.value + option.url
		}

	})

</script>
第四步:

使用

<template>
        <view @click="change_pdf(file_url)"> 点击查看附件 </view>  //file_url pdf文件路径
</template>



<script setup lang="ts">
	function change_pdf(url : any) {
		uni.navigateTo({
			url: `/pages/index/viewPdf?url=${url}`
		})
	}
</script>

注:ios可省略步骤 直接使用<web-view :src="文件路径"></web-view>  即可

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-08 05:50:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-08 05:50:07       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-08 05:50:07       82 阅读
  4. Python语言-面向对象

    2024-04-08 05:50:07       91 阅读

热门阅读

  1. 146 LRU缓存

    2024-04-08 05:50:07       36 阅读
  2. HTTP的强制缓存和协商缓存

    2024-04-08 05:50:07       36 阅读
  3. HTTPS中的TLS和TCP能同时握手吗

    2024-04-08 05:50:07       38 阅读
  4. GMSSL学习笔记

    2024-04-08 05:50:07       33 阅读
  5. 网络安全之SQL注入

    2024-04-08 05:50:07       35 阅读
  6. ubuntu18.04-arm7v架构下构建Telegraf自定义系统服务

    2024-04-08 05:50:07       32 阅读
  7. ubuntu怎么按安装时间显示已安装的软件

    2024-04-08 05:50:07       32 阅读
  8. 使用docx4j转换word为pdf处理中文乱码问题

    2024-04-08 05:50:07       33 阅读
  9. @SpringBootApplication 详解

    2024-04-08 05:50:07       33 阅读
  10. Springboot 集成Rabbitmq之延时队列

    2024-04-08 05:50:07       33 阅读