Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS,管理二进制数据_Javascript_Angularjs_Escaping_Download_Binaryfiles - Fatal编程技术网

Javascript AngularJS,管理二进制数据

Javascript AngularJS,管理二进制数据,javascript,angularjs,escaping,download,binaryfiles,Javascript,Angularjs,Escaping,Download,Binaryfiles,我想从ServiceSpring下载一个带有angularjs(1.0.8)的文件。我使用POST请求是因为我必须传递一段HTML作为参数,浏览器对查询字符串的长度有限制。这是我的代码: $http({ method: 'POST', url: '/export/pdf', data: "html=" + graphHtml.outerHTML, headers: {'Content-Type': 'application/

我想从ServiceSpring下载一个带有angularjs(1.0.8)的文件。我使用POST请求是因为我必须传递一段HTML作为参数,浏览器对查询字符串的长度有限制。这是我的代码:

    $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();
    });
我注意到收到的“数据”已经是“字符串”格式!我看到了许多(?)问题点,当类型

    typeof(data)

我收到“字符串”。我不想对我的原始数据进行这种解释。当我尝试在文件中写入数据时,大小是原始文件的两倍!我知道这是为了二进制数据的“字符串解释”,而不是希望以二进制形式读取。是否有人能解决以原始格式而不是字符串形式查看“数据”的问题?

在配置过程中设置$http responseType。在$http配置对象中,指定responseType:“blob”以获取以二进制格式返回的响应数据

responseType : "blob"
否则,默认响应格式为字符串。对于responseType的其他选择,请参阅AngularJS API参考。

这个答案可以通过总结链接的信息来改进,特别是在链接失效的情况下。我添加了responseType:“blob”,但servlet接收的数据仍然是UTF-8格式。下面是代码:$http({method:'PUT',url:'api/clients/“+$routeParams.clientId+scope.resource.path,数据:rsc,超时:300000,响应类型:“blob”,头:{'Content-Type':'multipart/form data'})