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 分钟前
为什么要有 TypeScript?让 JS 告别 “薛定谔的 Bug”
前端·javascript·面试
海天鹰1 小时前
SOC架构
javascript
墨染天姬1 小时前
【AI】如何基于cursor创建MCP索引pdf
人工智能·pdf
前进的李工1 小时前
MySQL角色管理:权限控制全攻略
前端·javascript·数据库·mysql
NPE~2 小时前
[App逆向]环境搭建下篇 — — 逆向源码+hook实战
android·javascript·python·教程·逆向·hook·逆向分析
洒满阳光的庄园2 小时前
Electron 桌面端打包流程说明
前端·javascript·electron
qq_429499572 小时前
分享免费的PDF 翻译 原格式
pdf
子琦啊2 小时前
【算法复习】数组与双指针篇
javascript·算法
来自外太空的鱼-张小张2 小时前
jeecg预览pdf、jeecg无法预览pdf、jeecg自带预览pdf
pdf·状态模式
SuperEugene2 小时前
前端通用基础组件设计:按钮/输入框/弹窗,统一设计标准|组件化设计基础篇
前端·javascript·vue.js·架构