微信公眾平臺 =》 開發(fā)管理 =》?開發(fā)設(shè)置 =》業(yè)務(wù)域名
注意:配置為業(yè)務(wù)域名后,可調(diào)用web-view組件在小程序中打開文章來源:http://www.zghlxwxcb.cn/news/detail-505447.html
注意:需要區(qū)分是安卓還是ios,ios可以直接使用webview打開pdf文件,安卓需要先下載再預(yù)覽文章來源地址http://www.zghlxwxcb.cn/news/detail-505447.html
// 打開pdf文件
// url pdf文件地址 title pdf文件名稱
export const downloadFileFunc = (url = '', title = '') => {
const reg = /(\w*)《(.*)》/g;
let str = url.split('/')[url.split('/').length - 1].split('.')[0];
const titleClone = title ? title.replace(reg, '$1 $2 ') : str;
uni.getSystemInfo({
success: function(sysRes) {
if (sysRes.platform === 'ios') {
jumpPage(
`/pages/webview/index?title=${encodeURIComponent(title)}&url=${encodeURIComponent(url)}`
);
} else {
uni.downloadFile({
url: url,
success: function(res) {
const filePath = res.tempFilePath
let arr = filePath.split('.');
const mime = arr[arr.length - 1];
const fs = uni.getFileSystemManager();
// 修改文件名字,僅安卓可以,ios無權(quán)限
fs.rename({
oldPath: filePath,
newPath: `${wx.env.USER_DATA_PATH}/${titleClone}`,
success() {
uni.openDocument({
filePath: `${wx.env.USER_DATA_PATH}/${titleClone}`,
fileType: mime,
success: function(res) {
console.log('打開文檔成功')
},
fail: function() {
uni.showToast({
title: '打開文件失敗',
icon: 'none',
duration: 2000
});
}
})
},
fail: function() {
uni.showToast({ title: '保存文件失敗', icon: 'none', duration: 2000 });
},
});
},
fail:function(error){
uni.showToast({
title: '下載文件失敗',
icon: 'none',
duration: 2000
});
}
})
}
},
fail: function(err) {
uni.showToast({
title: '獲取系統(tǒng)信息失敗',
icon: 'none',
duration: 2000
});
}
})
};
到了這里,關(guān)于uniapp 小程序打開預(yù)覽pdf文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!