uniapp获取 pdf文件流 并展示

1、流数据

uni.request({

url: this.$config.apiUrl+"/api/report/content/fill?code=bv.mf.refund.pay.voucher&busiNo=00201323051500148949",

header: {

'content-type': 'application/json;charset=utf-8',

'X-App-Code': 'weixin',

'X-Source': 'program',

'X-Token': store.getters.token

},

responseType:'arraybuffer',

success: (data) => {

const fs = uni.getFileSystemManager()

const filePath = wx.env.USER_DATA_PATH + '/' + Date.now() + '.pdf' // wx.env.USER_DATA_PATH 为微信提供的保存文件路径

console.log(filePath)

fs.writeFile({

filePath,

data: data.data, // 将 base64 转为 arrayuffer wx.base64ToArrayBuffer(res.replace(/[\r\n]/g, ''))

success (res) {

uni.openDocument({

showMenu: true,

fileType: 'pdf',

filePath,

success: function (res) {

console.log('打开文档成功')

}

})

},

fail (err) {

console.log('错误', err)

}

})

}

});

2 文件地址模式

复制代码
        uni.showLoading({
            title: '下载中'
        });
        //下载文件
        uni.downloadFile({ //只能是GET请求
            url:  this.$config.apiUrl+"/api/report/content/fill?code=bv.mf.refund.pay.voucher&busiNo=00201323051500148949", //请求地址(后台返回的码流地址)
           // url: this.$config.staticUrl+"/emptyOwner.png",
            success: (res) => {
                //下载成功
                if (res.statusCode === 200) {
                    //保存文件
                    let tempFile = res.tempFilePath;
                    //保存成功之后 打开文件
                    uni.getFileSystemManager().saveFile({
                        tempFilePath: tempFile,
                        // filePath: wx.env.USER_DATA_PATH + '/' + '上传成员.pdf',//自定义文件名
                        success(res) {
                            console.log(res)
                            uni.openDocument({
                                filePath: res.savedFilePath,
                                showMenu: true, //是否可以分享
                                success: (res) => {
                                    uni.hideLoading()
                                    console.log(res);
                                },
                                fail: (e) => {
                                    uni.showToast({
                                        title: '打开失败',
                                        icon: "error"
                                    })
                                }
                            })

                        }
                    })
                }
            },
            fail: (e) => {
                console.log(e, '文件下载失败')
                uni.showToast({
                    title: '文件下载失败',
                    icon: "error",
                })
            }
        });
相关推荐
东风西巷2 小时前
Rubick:基于Electron的开源桌面效率工具箱
前端·javascript·electron·软件需求
Miracle_G3 小时前
每日一个知识点:JavaScript 箭头函数与普通函数比较
javascript
unfetteredman3 小时前
Error: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.32' not found
前端·javascript·vite
2501_916007473 小时前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
拓端研究室3 小时前
专题:2025全球消费趋势与中国市场洞察报告|附300+份报告PDF、原数据表汇总下载
大数据·信息可视化·pdf
程序员小续4 小时前
React 官方严令禁止:Hook 不能写在 if/else,真相竟然是…
前端·javascript·程序员
小奋斗4 小时前
深入浅出:JavaScript中 三大异步编程方案以及应用
javascript·面试
尝尝你的优乐美4 小时前
封装那些Vue3.0中好用的指令
前端·javascript·vue.js
敲代码的彭于晏4 小时前
localStorage 不够用?试试 IndexedDB !
前端·javascript·浏览器
chxii4 小时前
5.4 4pnpm 使用介绍
前端·javascript·vue.js