uniapp 图片文件流

使用 uni.request 请求数据并处理返回的文件流

以下代码演示了如何使用 `uni.request` 发起 GET 请求,并处理返回的文件流数据。

uni.request({
    url: '*****', // 仅为示例,并非真实接口地址。
    method: 'GET',
    responseType: 'arraybuffer',
    data: {},
    header: {
        'content-type': 'application/json',
        'token': 'token'
    },
    success: (res) => {
        // 返回的是文件流
        let datas = res.data;
        let blob = new Blob([datas], {
            type: 'application/pdf;charset=UTF-8'
        });
        let pdfData = window.URL.createObjectURL(blob); // 创建预览路径
        let agreementUrl = encodeURIComponent(pdfData);

        let codeImg = 'data:image/png;base64,' + btoa(
            new Uint8Array(datas).reduce((datas, byte) => datas + String.fromCharCode(byte), '')
        );
    }
});

注意: 在此代码中,我们首先使用 `Blob` 对象将文件流转换为 PDF 格式,并创建一个预览路径。然后,我们将文件流转换为 Base64 格式的 PNG 图片。

posted on 2022-08-26 21:43  完美前端  阅读(2456)  评论(0)    收藏  举报

导航