I am Currently working on task to download file (PDF/excel/text) using secure API in my system in angular 2 (beta) version.
我目前正致力于在我的系统中以角度2(beta)版本使用安全API下载文件(PDF / excel / text)。
I have used post API with authentication header and trying to create blob using data bytes received.
我使用了带有身份验证标头的post API,并尝试使用收到的数据字节创建blob。
I have tried using following code
我尝试使用以下代码
return this.http.get(url, { headers: this.headers}).map( response => response.blob())
But, i got the error that blob method is not implemented in angular 2 HTTP.
但是,我得到的错误是在角度2 HTTP中没有实现blob方法。
so i am trying following code where i need to convert string to byte array.
所以我正在尝试下面的代码,我需要将字符串转换为字节数组。
return this.http.get(Configuration.API_URL + url, { headers: this.headers }).map(
response => {
var bytes = [];
var utf8 = encodeURIComponent(response.text());
for (var i = 0; i < utf8.length; i++) {
bytes.push(utf8.charCodeAt(i));
}
var data = new Blob([bytes], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(data);
window.open(fileURL);
}
);
here i am facing some issue with the bytes array. Byte array is not same as the one sent by the API.
在这里我面临一些字节数组的问题。字节数组与API发送的字节数组不同。
Need help in either converting string to byte array or using blob in angular 2 HTTP get request.
在将字符串转换为字节数组或在angular 2 HTTP get请求中使用blob时需要帮助。
1 个解决方案
#1
1
Probably, The below npm package can help you convert it into ByteArray.
可能,下面的npm包可以帮助您将其转换为ByteArray。
https://www.npmjs.com/package/xdata
Hope it helps you!
希望它能帮到你!
Cheers!
#1
1
Probably, The below npm package can help you convert it into ByteArray.
可能,下面的npm包可以帮助您将其转换为ByteArray。
https://www.npmjs.com/package/xdata
Hope it helps you!
希望它能帮到你!
Cheers!