使用uni.downloadFile、uni.previewImage、uni.openDocument實現文件預覽
uni.openDocument(OBJECT)官網描述
新開頁面打開文檔,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
平台差異說明
App | H5 | 微信小程序 | 支付寶小程序 | 百度小程序 | 字節跳動小程序、飛書小程序 | QQ小程序 | 快手小程序 |
---|---|---|---|---|---|---|---|
√ | x | √ | √ | √ | √ | √ | x |
OBJECT 參數說明:
參數名 | 類型 | 必填 | 說明 | 平台差異說明 |
---|---|---|---|---|
filePath | String | 是 | 文件路徑,可通過 downFile 獲得 | |
fileType | String | 否 | 文件類型,指定文件類型打開文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx | 微信小程序 |
showMenu | Boolean | 否 | 右上角是否有可以轉發分享的功能 | 微信小程序 |
success | String | 否 | 接口調用成功的回調函數 | |
fail | String | 否 | 接口調用失敗的回調函數 | 微信小程序 |
complete | String | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
代碼:
uni.downloadFile({ url: this.sjuAjax.fileUrl+fileUrl, success: function (res) {
if(res.tempFilePath.indexOf(".png" ||".jpg") > -1
)
{
var filePath = []
filePath[0]=res.tempFilePath;
// 預覽圖片
uni.previewImage({
urls: filePath,
success: function (res) {
console.log('打開成功')
},
fail:function(res){
console.log(res)
}
})
}
else
{
var filePath = res.tempFilePath;
uni.openDocument({
filePath: filePath,
success: function (res) {
console.log('打開文檔成功');
}
});
}
}, fail:function (res){ console.log(res) } });