1.實(shí)現(xiàn)思路:
這里咱們直接用的uniapp官方提供的uni.downloadFile方法調(diào)用手機(jī)第三方能打開文件的工具,比如wps等(ps:這里實(shí)現(xiàn)的是APP文件預(yù)覽)。
2. 直接上代碼(可直接復(fù)制)
//文件預(yù)覽
prefile(e){
let that = this
let url = that.$config.fileUrl+e;
uni.downloadFile({
url: url,
success: function (res) {
let filepathss=plus.io.convertLocalFileSystemURL(res.tempFilePath);
setTimeout(
() =>
uni.openDocument({
filePath: filepathss,
showMenu: false,
success: function () {
console.log("打開文檔成功");
},
fail: function () {
uni.showToast({
title: '暫不支持此類型',
duration: 2000,
icon: "none",
});
}
}),
1000
);
},
fail: function (res) {
console.log(res); //失敗
}
});
}
3.注意事項(xiàng)
1.文件路徑(url)必須是瀏覽器能直接訪問的文件。比如:http://xx.cc.com/images/abc.xlsx 這種格式。 最開始我是用的后臺給的接口 file/dowload?fileId=112334 這種形式的路徑,調(diào)用 uni.openDocument 是失敗的,原因可能是無法識別文件類型。
2.注意代碼中
let filepathss=plus.io.convertLocalFileSystemURL(res.tempFilePath);
這行代碼是獲取文件在手機(jī)中的絕對路徑。文章來源:http://www.zghlxwxcb.cn/news/detail-516241.html
例如:文章來源地址http://www.zghlxwxcb.cn/news/detail-516241.html
到了這里,關(guān)于uniApp 使用uni.openDocument(object)預(yù)覽pdf、excel、word等文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!