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",
                })
            }
        });
相关推荐
遂心_4 小时前
JavaScript 函数参数传递机制:一道经典面试题解析
前端·javascript
小徐_23334 小时前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts
遂心_5 小时前
深入理解 React Hook:useEffect 完全指南
前端·javascript·react.js
前端Hardy5 小时前
HTML&CSS: 谁懂啊!用代码 “擦去”图片雾气
前端·javascript·css
前端Hardy5 小时前
HTML&CSS:好精致的导航栏
前端·javascript·css
一个不爱写代码的瘦子6 小时前
迭代器和生成器
前端·javascript
iOS阿玮9 小时前
永远不要站在用户的对立面,挑战大众的公知。
uni-app·app·apple
源猿人9 小时前
企业级文件浏览系统的Vue实现:架构设计与最佳实践
前端·javascript·数据可视化
RoyLin9 小时前
TypeScript设计模式:迭代器模式
javascript·后端·node.js
xw510 小时前
uni-app中v-if使用”异常”
前端·uni-app