背景
在使用web-view时,发现需要展示的页面是一个PDF、png、jpg、word等,所以安卓不支持访问,这个时候我们就需要使用微信的另外一个方法downloadFile和openDocument这两个方法,用于将文件临时下载,然后进行展示
方法
自行修改相对应的文件路径,然后记得真机测试
get_shuoms : function(e){
var file_name=this.data.shuoms_file;
var url=app.globalData.URL;
const contractUrl =url+"pivas/res?filename="+file_name;
console.dir(contractUrl)
//这个是用来拿到要跳转的url链接的
const length = contractUrl.length;
const newstring = contractUrl.substring(length - 4, length);
//裁剪出来链接文件的后缀,
const newstring2 = newstring.toLowerCase()
//toLowerCase()把字符串转换为小写,因为pdf格式的文件可能大写,也可能人为改后缀大小写穿插
if (newstring2 == '.pdf') {
wx.downloadFile({
// 示例 url,并非真实存在
url: contractUrl,
success: function(res) {
console.dir(res)
const filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
success: function(res) {
console.log('打开文档成功')
}
})
}
})
} else {
wx.downloadFile({
// 示例 url,并非真实存在
url: contractUrl,
success: function(res) {
console.dir(res+"222222222");
const filePath = res.tempFilePath;
console.dir(filePath);
wx.openDocument({
filePath: filePath,
success: function(res) {
console.log('打开文档成功')
}
})
}
})
}
},
疑问
当遇到的是pdf时,依托于qq浏览器即可,但是其他文件(png、jpg等需要三方应用的支持)