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",
                })
            }
        });
相关推荐
不好听6135 分钟前
JavaScript 的 this 到底指向谁?
javascript·面试
触底反弹7 分钟前
🔥 2026 年爆火的 Harness Engineering 到底是什么?从原理到实战一文讲透
javascript·人工智能·程序员
mONESY8 分钟前
一文搞定JavaScript不同场景中 this 的指向问题
javascript
用户2986985301418 分钟前
在 React 中使用 JavaScript 合并 Excel 文件
前端·javascript·react.js
大流星21 分钟前
LangChainJs之基础模型(一)
javascript·langchain
橘子星23 分钟前
JavaScript this 指向全解实战指南
前端·javascript
weedsfly25 分钟前
JS垃圾回收:从原理到项目实战,彻底根治内存泄漏
前端·javascript·面试
万少12 小时前
万少的博客 - 技术分享与解决方案
前端·javascript·后端
尘世中一位迷途小书童15 小时前
用 Cesium 撸了一个森林火情监控大屏,弧线、粒子、发光效果都齐了
前端·javascript
先吃饱再说16 小时前
JavaScript中`this` 的“千层套路”:从默认绑定到箭头函数的五种指向
javascript