angularjs有一些像fetch()。blob这样的技术

时间:2017-08-24 17:47:44

标签: javascript angularjs

为音频/ mp3文件调用$ http.get()

来自chrome网络响应的

我看到响应数据如下所示:

“ID3” TSSELavf56.4.101ÿû@ÀInfo\Ê

“$”),. 1469<> ACEHJMORUWZ] _bdgilnqsvx {〜¢¤§©¬®±³¶¸»½ÀÃÅÈÊÍÏÒÔ×ÙÜÞáäçéìîñóöøûýLavf56.4.101$ÿûPÄÀ¤4LAME3.98.4UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUULAME3.98.4UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUÿûRÄ]夓

我不下载或播放。

但是使用Javascript fetch()。blob() 我可以将它转换为Blob 来自fetch(响应)我知道数据类型是ReadableStream

我的问题是,如何使用angularjs来处理这些数据

1 个答案:

答案 0 :(得分:0)

如何使用AngularJS下载二进制文件

下载二进制文件时,设置responseType

非常重要
app.service('dataService',['$http',function($http) {

    this.downloadFile = function(url) {

        var config = {      
            //SET responseType
            responseType: 'blob',
        };

        return $http.get(url, config);
    } 
]);

如果省略responseType,则XHR API默认将UTF-8编码文本转换为DOMString (UTF-16),这会损坏PDF,图像和其他二进制文件。

有关详细信息,请参阅MDN Web API Reference - XHR ResponseType