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",
                })
            }
        });
uniapp获取文件有以下几种方式: 1. 使用uniapp官方提供的uni.downloadFile()方法下载文件,然后使用uni.getFileSystemManager()方法获取文件系统管理器,通过文件系统管理器读取文件内容,最终获取文件。 示例代码: ```javascript uni.downloadFile({ url: 'https://example.com/file.pdf', success: (res) => { if (res.statusCode === 200) { const filePath = res.tempFilePath; const fileManager = uni.getFileSystemManager(); fileManager.readFile({ filePath: filePath, success: (res) => { const fileStream = res.data; // 获取文件 } }); } } }); ``` 2. 使用uni.chooseMessageFile()方法选择文件,然后使用uni.getFileSystemManager()方法获取文件系统管理器,通过文件系统管理器读取文件内容,最终获取文件。 示例代码: ```javascript uni.chooseMessageFile({ count: 1, type: 'file', success: (res) => { const filePath = res.tempFiles[0].path; const fileManager = uni.getFileSystemManager(); fileManager.readFile({ filePath: filePath, success: (res) => { const fileStream = res.data; // 获取文件 } }); } }); ``` 3. 使用uni.chooseImage()方法选择图片,然后使用uni.getFileSystemManager()方法获取文件系统管理器,通过文件系统管理器读取图片文件内容,最终获取图片文件。 示例代码: ```javascript uni.chooseImage({ count: 1, success: (res) => { const filePath = res.tempFilePaths[0]; const fileManager = uni.getFileSystemManager(); fileManager.readFile({ filePath: filePath, encoding: 'base64', success: (res) => { const fileStream = 'data:image/png;base64,' + res.data; // 获取文件 } }); } }); ``` 以上就是在uniapp获取文件的几种方式,根据实际需求选择相应的方法即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值