笔记 -- uniapp 微信小程序webview安卓文件预览失败

uniapp 微信小程序webview安卓文件预览失败

uniapp 微信小程序 webview前 要判断设备 是安卓 还是苹果
是安卓直接下载打开文件
是苹果跳转webview预览

//获取设备类型
uni.getSystemInfo({
	success: res => {
		console.log(res.platform);
		if (res.platform === 'android') {
			uni.downloadFile({
				url: config.Abc_Xieyi+'/abc_xieyi.pdf',
				success: function(res) {
					const filePath = res.tempFilePath;
					uni.openDocument({
					filePath: filePath,
					success: function(res) {
						console.log('打开文档成功');
					}
				});
				}
			});
		} else {
			uni.navigateTo({
				url: '/pages/webview_pdf'//跳转webview
			})
		}
	}
})

webview代码

<template>
	<view>
		<web-view :webview-styles="webviewStyles" :src="Http"></web-view>
	</view>
</template>

<script>
	import config from "@/util/config.js";
	export default {
		data() {
			return {
				Http:'',
				webviewStyles: {
					progress: {
						color: '#FF3333'
					}
				},
			}
		},
		onLoad(){
			this.Http = config.Abc_Xieyi+'/abc_xieyi.pdf';
		},
		onShow() {
		
		},
	}
</script>

<style>
</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序webview中,要实现文件下载功能,可以通过以下步骤进行操作: 1. 在小程序webview中,可以使用标准的HTML元素`<a>`来触发文件下载。首先,在小程序的webview页面中,添加一个下载按钮或者链接,例如: ```html <a href="https://example.com/path/to/file.pdf" download>点击下载文件</a> ``` 其中`https://example.com/path/to/file.pdf`是要下载的文件的URL,`download`属性表示要下载文件而不是在浏览器中打开。 2. 在小程序的webview页面的JS代码中,可以监听这个下载链接的点击事件,并在点击时触发文件下载。例如: ```javascript document.querySelector('a').addEventListener('click', function(e) { e.preventDefault(); // 阻止默认的链接跳转行为 var url = this.getAttribute('href'); wx.downloadFile({ url: url, success: function(res) { var filePath = res.tempFilePath; // 下载后的临时文件路径 wx.saveFile({ tempFilePath: filePath, success: function(res) { var savedFilePath = res.savedFilePath; // 保存后的文件路径 // 文件保存成功后的操作 }, fail: function(res) { // 文件保存失败后的操作 } }); }, fail: function(res) { // 文件下载失败后的操作 } }); }); ``` 以上代码中,`document.querySelector('a')`用于获取第一个`<a>`元素,根据实际情况可能需要修改选择器;`wx.downloadFile`用于下载文件,`wx.saveFile`用于保存文件到本地。 需要注意的是,下载文件需要在小程序的`app.json`配置文件中添加相应的权限设置,例如: ```json { "mp-weixin": { "permission": { "scope.userLocation": { "desc": "获取您的地理位置信息将用于小程序定位" }, "scope.writePhotosAlbum": { "desc": "保存图片到相册" }, "scope.camera": { "desc": "拍摄照片或者录像" }, "scope.record": { "desc": "录制音频" }, "scope.userInfo": { "desc": "获取您的基本信息将用于小程序登录" }, "scope.userLocationBackground": { "desc": "获取您的地理位置信息将用于小程序定位" }, "scope.invoiceTitle": { "desc": "获取你发票抬头" }, "scope.invoice": { "desc": "获取你发票" }, "scope.werun": { "desc": "微信运动步数" }, "scope.writeVideosAlbum": { "desc": "保存视频到相册" } } } } ``` 以上是在微信小程序webview中实现文件下载的一种方式,你可以根据具体需求进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值