需求:通过接口返回的二进制流数据,这个流数据他是一个xlsx文档,需要给到用户一个文档线上连接。

下面是具体代码,注意只针对二进制的文件数据,如果图片上传直接调用uploadFile就可以,并且兼容原生微信小程序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
export function exportExcel1(query) {
    uni.showLoading({
        title: '正在导出...',
        mask: true
    });
    // 这个是get接口参数 setUrlQuery 是我自定义的函数封装 主要是拼接url参数的
    let obj = {
        "personnelIdList":state.personnel.list.map((item)=>item.personnelId),
        "projectId":store.project.projectId,
        ...query
    };
    // 调用接口 获取二进制流 进行下载并且获取临时文件地址
    uni.downloadFile({
        url:env.ApiUrl+'/personnel/laborer/exportExcel'+uni.setUrlQuery(obj),
        header: {
            'content-type': 'application/json',
            'Authorization' : 'Bearer ' + storage.getItem('Authorization')
        },
        success(res){
            let tempFilePath = res.tempFilePath; // 获取微信返回的临时系统文件地址
            uni.uploadFile({
                url: env.ApiUrl + 'xxx', // 上传文件接口
                formData: {}, // 除文件外其他所有数据,传对象,会默认转换为 FormData
                filePath: tempFilePath, // 上传临时的系统文件地址
                header: {
                    'content-type': 'application/json',
                    'Authorization' : 'Bearer ' + storage.getItem('Authorization')
                },
                name: 'file', // 注意与后端约定的字段名称
                success(res){
                    uni.hideLoading();
                    // 弹窗提示导出成功 用户点击确定复制url文件地址
                    uni.showModal({
                        title: '导出成功',
                        content: '已生成当前列表人员数据文档!',
                        cancelText: '取消',
                        confirmText: '复制URL',
                        success: function (res) {
                            if (res.confirm) {
                                // 获取url 进行复制
                                uni.setClipboardData({
                                    data:result,
                                    success() {},
                                });
                            }
                        }
                    });
                    // 文件预览
                    // uni.openDocument({
                    //  filePath: 'url',// 可以是微信给的系统临时url地址,也可以是正式的线上地址
                    //  success: (sus) => {
                    //      console.log('成功打开');
                    //  },
                    // });
                }
            });
        }
    });
};

 

posted on   马丁的车夫  阅读(1802)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示