官網(wǎng):
wx.openDocument(Object object) | 微信開放文檔微信開發(fā)者平臺文檔https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.openDocument.html
說的fileType是可選參數(shù)!
?但是不加上的話,就打不開!
加上之后就能打開了。
?關(guān)鍵代碼:
wx.openDocument({
filePath: filePath,
fileType: fileType,
showMenu: true,
success: function(res) {
console.log('打開文檔成功');
},
fail: function(error) {
wx.showToast({
icon: 'none',
title: '打開文件失敗'
});
},
});
測試數(shù)據(jù):先從文件的url里面篩選文件格式:
let ss='https://www.baidu.com/f85284da55164de2a2d9cb0b62fabab1.pdf?response-content-disposition=attachment%3Bfilename%3D%E6%9D%A8%E5%AD%90%E5%B9%BF%E5%91%8A%E5%87%BA%E8%B4%A7%E5%8D%95.pdf&AWSAccessKeyId=XIDM2OFRLLDUR8YFJJWM&Expires=1679793304&Signature=WcNl5HmwWOvNU%2F51f%2F5ZQ1h9BbA%3D'
ss.substring(ss.lastIndexOf(".")+1).split('&')[0]
'pdf'
demo:?
/**
* 微信小程序只支持fileType這些類型:
* doc doc 格式
* docx docx 格式
* xls xls 格式
* xlsx xlsx 格式
* ppt ppt 格式
* pptx pptx 格式
* pdf pdf 格式
* @param url
*/
downloadFile(url) {
url='https://www.baidu.com/f85284da55164de2a2d9cb0b62fabab1.pdf?response-content-disposition=attachment%3Bfilename%3D%E6%9D%A8%E5%AD%90%E5%B9%BF%E5%91%8A%E5%87%BA%E8%B4%A7%E5%8D%95.pdf&AWSAccessKeyId=XIDM2OFRLLDUR8YFJJWM&Expires=1679793304&Signature=WcNl5HmwWOvNU%2F51f%2F5ZQ1h9BbA%3D'
console.log('url', '---的值是?', url)
const sufInx = url.lastIndexOf(".");
if (sufInx === -1) {
uni.showModal({
title: '提示信息',
content: '文件錯誤!',
success: function(res) {
if (res.confirm) {
//console.log('用戶點擊確定');
} else if (res.cancel) {
//console.log('用戶點擊取消');
}
}
});
return false
}
let fileType = ''
console.log('sufInx', '---的值是?', sufInx)
fileType = url.substring(sufInx + 1).split('&')[0]
console.log('fileType', '---的值是?', fileType)
let fileTypeArr = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf']
console.log('fileType', '---的值是?', fileType)
if (!fileTypeArr.includes(fileType)) {
uni.showModal({
title: '提示信息',
content: '只能打開word/excel/ppt/pdf文件(比如:doc, docx, xls, xlsx, ppt, pptx, pdf這些類型的文件)!',
success: function(res) {
if (res.confirm) {
//console.log('用戶點擊確定');
} else if (res.cancel) {
//console.log('用戶點擊取消');
}
}
});
return false
} else {
wx.downloadFile({
url: url, //僅為示例,并非真實的資源
success: (res) => {
if (res.statusCode === 200) {
console.log('下載成功--res', res);
var filePath = res.tempFilePath;
wx.openDocument({
filePath: filePath,
fileType: fileType,
showMenu: true,
success: function(res) {
console.log('打開文檔成功');
},
fail: function(error) {
wx.showToast({
icon: 'none',
title: '打開文件失敗'
});
},
});
}
},
fail: function(err) {
console.log('fail')
console.log(err)
wx.showToast({
icon: 'none',
title: '下載文件失敗'
});
}
});
}
},
最后,pdf打開成功:
?文章來源地址http://www.zghlxwxcb.cn/news/detail-505339.html文章來源:http://www.zghlxwxcb.cn/news/detail-505339.html
?
到了這里,關(guān)于微信小程序踩坑:wx.openDocument(Object object)打不開文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!