外部API接口返回是附件会报错

背景&操作步骤

做了什么操作后,出现的该问题

  1. 外部API接口返回是附件的时候,服务代码里调用这个API会报错

问题

页面截图

前端控制台错误截图 (如果只是后端问题,可跳过)

接口返回数据截图 & 后端日志错误提示(如只是前端问题,可跳过)

期望效果

你这个download接口返回的是什么,截图看下

image

封装成flask 的 response对象返回

如果你的终极目的是在极态上点击后, 能获取到其他服务器的文件并自动下载, 那么没必要用服务函数调用外部api终转一下。 可以参考下面的这个链接: 揭秘jQuery AJAX接收二进制流:轻松处理文件下载与图片展示 - 云原生实践 在极态的前端直接调远程服务, 然后远程服务的cors 对你的极态的ip放开。 前端通过jquery 直接请求数据,然后生成a链接,模拟人 click 就能触发下载了。

操作是有个合同审批后,将合同数据通过api同步到签章平台,根据签章平台返回的id继续通过api返回文件同步到jit合同的附件字段 。 目前已经通过服务函数同步了api也取到了 二进制文件 无法同步到jit合同的附件字段。

@wanjianjun

这个目前后台没有现成可用的方法,可通过 svc.app.getElement(“storages.services.StorageSvc”)
svc.uploadByFile(文件名,文件内容) 上传后返回 FileModel对象,.value后得到
{
“uid”: uid,
“storeFullName”: storeFullName,
“uploadTime”: uploadTime,
“expires”: expireDate,
“md5”: md5,
“fileName”: fileName,
“url”: url,
}
然后封装成附件字段存储的格式(上传附件看接口返回结果查看格式),赋值给附件字段。

保存到jit磁盘的文件得到的url无法打开

附件字段的赋值有demo文档可以参考吗, 没有文档我们不知道怎么赋值进去c507cde2365b9c88ddeaf4f53699db08

@wanjianjun

这是我们下载图片的按钮。

/**
 * 下载文件
 * @param url
 * @param name
 */

import { message } from 'antd';

export function downloadFile(url: string, name: string): void {
    const ua = navigator.userAgent;
    if (ua.includes('DingTalk')) {
        try {
            (window as any)?.dd?.biz?.util?.downloadFile({
                url,
                name: name, //定义下载文件名字
                onFail: function (err: any) {
                    console.log('err', err);
                    message.error(err);
                },
            });
        } catch (error: any) {
            console.log('error', error);
            message.error(error.message);
        }

        return;
    }
    // 用fetch发送请求
    fetch(url)
        .then((res) => {
            res.blob().then((blob) => {
                const blobUrl = window.URL.createObjectURL(blob);
                // 这里的文件名根据实际情况从响应头或者url里获取
                const filename = name;
                const a = document.createElement('a');
                a.href = blobUrl;
                a.download = filename;
                a.click();
                window.URL.revokeObjectURL(blobUrl);
            });
        })
        .catch(() => {
            const node = document.createElementNS(
                'http://www.w3.org/1999/xhtml',
                'a'
            ) as any;
            node.href = url;
            node.download = name;
            const ev = document.createEvent('MouseEvents');
            ev.initMouseEvent(
                'click',
                true,
                false,
                window,
                0,
                0,
                0,
                0,
                0,
                false,
                false,
                false,
                false,
                0,
                null
            );
            node.dispatchEvent(ev);
        });
}

这个要研究前端页面开发,我们暂时没那么多时间去熟悉。

从操作上我们也不可能在页面上去再次触发,业务人员不接受这种方式

这样, 你在数据模型里面, 有附件字段的, 手动上传一个附件,按照data的格式来构造数据, 更新到数据模型中试试。

好的 我们测试一下

谢谢大佬,已经解决了。