背景&操作步骤
做了什么操作后,出现的该问题
- 外部API接口返回是附件的时候,服务代码里调用这个API会报错
做了什么操作后,出现的该问题
你这个download接口返回的是什么,截图看下

封装成flask 的 response对象返回
如果你的终极目的是在极态上点击后, 能获取到其他服务器的文件并自动下载, 那么没必要用服务函数调用外部api终转一下。 可以参考下面的这个链接: 揭秘jQuery AJAX接收二进制流:轻松处理文件下载与图片展示 - 云原生实践 在极态的前端直接调远程服务, 然后远程服务的cors 对你的极态的ip放开。 前端通过jquery 直接请求数据,然后生成a链接,模拟人 click 就能触发下载了。
操作是有个合同审批后,将合同数据通过api同步到签章平台,根据签章平台返回的id继续通过api返回文件同步到jit合同的附件字段 。 目前已经通过服务函数同步了api也取到了 二进制文件 无法同步到jit合同的附件字段。
这个目前后台没有现成可用的方法,可通过 svc.app.getElement(“storages.services.StorageSvc”)
svc.uploadByFile(文件名,文件内容) 上传后返回 FileModel对象,.value后得到
{
“uid”: uid,
“storeFullName”: storeFullName,
“uploadTime”: uploadTime,
“expires”: expireDate,
“md5”: md5,
“fileName”: fileName,
“url”: url,
}
然后封装成附件字段存储的格式(上传附件看接口返回结果查看格式),赋值给附件字段。
这是我们下载图片的按钮。
/**
* 下载文件
* @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);
});
}
这个要研究前端页面开发,我们暂时没那么多时间去熟悉。
从操作上我们也不可能在页面上去再次触发,业务人员不接受这种方式
好的 我们测试一下
谢谢大佬,已经解决了。