【发布时间】:2019-11-26 05:30:35
【问题描述】:
我从我的服务器获得了一个带有文件内容的byte[] 文件。我知道名字和content-type。到目前为止,我已经尝试了以下方法来下载文件:
const a = document.createElement('a');
document.body.appendChild(a);
a.style.display = 'none';
const file = new Blob([content], {type: 'text/plain'});
const url = window.URL.createObjectURL(file);
a.href = url;
a.download = "test.txt";
a.click();
window.URL.revokeObjectURL(url);
但是这个解决方案只是下载一个包含二进制内容的文本文件。如何使用 JavaScript/Typescript 在客户端将二进制数据转换为对应的文件类型?谢谢!
【问题讨论】:
标签: javascript angular typescript file download