可以預覽並下載如下格式:doc, xls, ppt, pdf, docx, xlsx, pptx
其實過程很簡單,可以實現預覽和下載兩個功能
1、拿到一個可以下載的url連接
2、uni.downloadFile獲取文件的臨時路徑
3、uni.openDocument打開文件,進入打開文件的界面後就可以另存為了
let url = `https://test//file/docx/testfile.docx`;
uni.downloadFile({
url: url,
success: function (res) {
// 拿到臨時路徑
let filePath = res.tempFilePath;
// 打卡文件
uni.openDocument({
filePath: filePath,
showMenu: true,
success: function (res) {
console.log('打開文檔成功', res);
},
fail: function (err) {
console.log('打開失敗', err);
}
});
}
});
PC端是直接通過默認docx\pdf文件瀏覽器打開
小程序端則是直接調用微信內置預覽界面預覽,右上角更多操作裏可以下載或轉發,都是保存的一種方式
參考資料
uni.downloadFile(OBJECT)
uni.openDocument(OBJECT)