二进制文件下载角度为2

时间:2016-10-12 09:08:52

标签: angular

我需要使用角度2从asp.net核心应用程序下载二进制文件。我发现了几个样本但是已经过时了,似乎没有一个更新为rtm。   是否有任何样品使用角度2 rtm?

2 个答案:

答案 0 :(得分:0)

Angular2 2.0.0中,您可以使用arrayBuffer()方法从下载的数据中获取二进制数组。然后你可以按照自己的意愿操纵它。

this.http.get("your/binary/uri").subscribe(res => {
    //Here you get your binary as an arraybuffer, do what you want with it.
    let data = res.arrayBuffer();
}

答案 1 :(得分:0)

I found this solution works for filestream returned by API.

this.http.get(this.apiurl, {headers: headers, responseType: 'arraybuffer'} )
        .subscribe(res => {
            let file = new Blob([res], { type: 'application/binary' });
            let fileURL = URL.createObjectURL(file);
            let fileLink = document.createElement('a');
            fileLink.href = fileURL;
            fileLink.download = "example.xlsx";
            fileLink.click();
        }
    );