最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS, manage binary data - Stack Overflow

programmeradmin2浏览0评论

I want to download a file with angularjs (1.0.8) from a service Spring. I use a POST request because I have to pass a piece of HTML as parament, and browsers have limitation with length of query string. Here my code:

    $http({
        method: 'POST',
        url: '/export/pdf',
        data: "html=" + graphHtml.outerHTML,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        transformResponse: function(data, headersGetter){
            return data;
        }
    }).success(function(data) {
        console.log("Type '" + typeof(data) + "'");
        var hiddenElement = document.createElement('a');
        hiddenElement.href = 'data:application/pdf,' + data;
        hiddenElement.target = '_blank';
        hiddenElement.download = 'myFile.pdf';
        hiddenElement.click();
    });

I notice that the "data" received is already in "string" format! I see many (?) question point, and when type

    typeof(data)

i receive "string". I don't want this interpretation of my raw data. When i try to write data in a file, the size is double respect original file! I know it is for "string interpretation" of binary data that instead wanted read as binary. Has anyone a solution for see "data" in raw format and not as string?

I want to download a file with angularjs (1.0.8) from a service Spring. I use a POST request because I have to pass a piece of HTML as parament, and browsers have limitation with length of query string. Here my code:

    $http({
        method: 'POST',
        url: '/export/pdf',
        data: "html=" + graphHtml.outerHTML,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        transformResponse: function(data, headersGetter){
            return data;
        }
    }).success(function(data) {
        console.log("Type '" + typeof(data) + "'");
        var hiddenElement = document.createElement('a');
        hiddenElement.href = 'data:application/pdf,' + data;
        hiddenElement.target = '_blank';
        hiddenElement.download = 'myFile.pdf';
        hiddenElement.click();
    });

I notice that the "data" received is already in "string" format! I see many (?) question point, and when type

    typeof(data)

i receive "string". I don't want this interpretation of my raw data. When i try to write data in a file, the size is double respect original file! I know it is for "string interpretation" of binary data that instead wanted read as binary. Has anyone a solution for see "data" in raw format and not as string?

Share Improve this question asked Mar 11, 2014 at 16:58 user3402702user3402702 911 silver badge11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

In a $http configuration object, specify

responseType : "blob"
to get response data returned in a binary format.

Otherwise, default response format is a String. For other choices for responseType, see AngularJS API reference.

set $http responseType in during config. and do something like this

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论