我通过一番挖掘和反复试验找到了解决办法。我想这将是一个惊喜,它可以如此容易地下载一个文件和那篇文章
createObjectURL()
this.http.post(
this.url + this.currentId,
params,
httpOptions
).subscribe((response: any) => {
const link = document.createElement('a');
link.setAttribute('style', 'display: none');
link.href = 'data:attachment/csv,' + encodeURI(response); // <- it was as simple as this.
const date = new Date();
link.download = this.currentChoice + '-' + date.getDate() + '-' + (1 + date.getMonth()) + '-' + date.getFullYear() + '.csv';
link.click();
link.remove();
}, error => {
console.log('File download failed : ', error);
});
我的api的返回已经是我不需要调用的文件的原始内容
.text()
或者其他孩子。
希望这有帮助:)