最近寫的uniapp項目需要新增一個pdf下載和打開查看功能,摸索了半天終于寫了出來,現(xiàn)分享出來供有需要的同行參考,歡迎指正
async function DownloadSignature() {
//請求后端接口,返回值為一個url地址
let res=await req.flow.flowDownload(data.flowId)
uni.downloadFile({
//res.data就是返回的地址
url: res.data,
success: function (res) {
if (res.statusCode === 200) {
// 下載成功
const filePath = res.tempFilePath; // 下載后的臨時文件路徑
uni.saveFile({
tempFilePath: filePath,
success: function (listRes) {
//此API是為了拿到文件保存的詳細路徑
plus.io.requestFileSystem( plus.io.PRIVATE_DOC, function( fs ) {
// 拿到 _doc 所在的完整路徑
console.log(fs.root.fullPath);
//彈出文件保存路徑
http.hint('文件保存成功'+fs.root.fullPath+listRes.savedFilePath,3000)
}, function ( e ) {
console.log( "Request file system failed: " + e.message );
} );
//保存成功后,調(diào)用文件打開方法
uni.openDocument({
filePath,
success: function(file) {
console.log('file-success',file);
}
});
},
fail: function (err) {
// 保存文件失敗
uni.showToast({
title: '保存文件失敗',
icon: 'none'
});
}
});
} else {
// 下載失敗
uni.showToast({
title: '下載失敗',
icon: 'none'
});
}
},
fail: function (err) {
// 下載請求失敗
uni.showToast({
title: '下載請求失敗',
icon: 'none'
});
}
});
}
因為功能寫的緊急,還沒有摸索出文件重命名方法(因為下載后的文件名是時間戳,這屬于優(yōu)化的一部分)以及自定義文件保存路徑方法,
效果圖:
文章來源:http://www.zghlxwxcb.cn/news/detail-622917.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-622917.html
到了這里,關(guān)于uniapp項目的pdf文件下載與打開查看的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!