https://developers.weixin.qq.com/s/ZhmyWqmF76jx
上面链接为:代码片段。
onLoad: function () {
var that = this;
// doc可以打开,pdf打不开,提示:无法打开“测试.pdf”!因为不支持该类型文件或已损坏。
// let fileName = "测试.doc";
// let url = "https://infinity-dev.sciicloud.com/dev-infinity/dd797d70be2c447ba5942e0bbfb6565e/material/2e22b3c069b64a659a9bcbf9635dfb9f.doc";
let fileName = "测试.pdf";
let url = "https://infinity-dev.sciicloud.com/dev-infinity/dd797d70be2c447ba5942e0bbfb6565e/material/70a708146de14cd886c7bb7352fd4e60.pdf";
var index1=url.lastIndexOf(".");
var index2=url.length;
var suffix=url.substring(index1,index2).toLowerCase();
var type=url.substring(index1+1,index2).toLowerCase();
var subUrl=url.substring(0,index1);
// debugger
wx.downloadFile({
url: url,
filePath: wx.env.USER_DATA_PATH + "/" + fileName,
success (res) {
wx.openDocument({
filePath: res.filePath,
fileType: type,
showMenu: true,
success (e) {
console.log("打开文档成功 =>", e);
wx.hideLoading();
},
fail (e) {
wx.hideLoading();
wx.showToast({
title: "下载文档失败!",
icon: "none"
});
}
});
},
fail (e) {
wx.hideLoading();
wx.showToast({
title: "下载文档失败!",
icon: "none"
});
}
});
},
模拟器和手机度不开。但是打印了“打开文档成功”