最近写的uniapp项目需要新增一个pdf下载和打开查看功能,摸索了半天终于写了出来,现分享出来供有需要的同行参考,欢迎指正
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 );
} );
//保存成功后,调用文件打开方法
uni.openDocument({

文章介绍了在uniapp项目中如何实现PDF的下载和查看。通过请求后端接口获取URL,使用uni.downloadFile进行下载,成功后利用uni.saveFile保存文件,并用uni.openDocument打开查看。目前存在的优化点包括文件重命名和自定义保存路径。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



