加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 在类型脚本中将字符串/文本转换为字节数组

发布时间:2020-12-17 06:59:05 所属栏目:安全 来源:网络整理
导读:我目前正致力于在我的系统中以角度2(beta)版本使用安全API下载文件(PDF / excel / text). 我使用了带有身份验证标头的post API,并尝试使用收到的数据字节创建blob. 我尝试使用以下代码 return this.http.get(url,{ headers: this.headers}).map( response =
我目前正致力于在我的系统中以角度2(beta)版本使用安全API下载文件(PDF / excel / text).

我使用了带有身份验证标头的post API,并尝试使用收到的数据字节创建blob.

我尝试使用以下代码

return this.http.get(url,{ headers: this.headers}).map( response => response.blob())

但是,我得到的错误是在角度2 HTTP中没有实现blob方法.

所以我正在尝试下面的代码,我需要将字符串转换为字节数组.

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);
            }
        );

这里我面临一些字节数组的问题.字节数组与API发送的字节数组不同.

在将字符串转换为字节数组或在angular 2 HTTP get请求中使用blob时需要帮助.

解决方法

可能,下面的npm包可以帮助您将其转换为ByteArray.

https://www.npmjs.com/package/xdata

希望它能帮到你!

干杯!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读