从HTTP响应保存在角文件 [英] Save a file in angular from a http response

查看:124
本文介绍了从HTTP响应保存在角文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我怎么可以保存包含在从角服务器的响应文件? (这样当响应到达该文件自动下载)

编辑:

我有一个$ HTTP POST方法,我在响应获取PDF数据。如果成功,我要响应数据保存为PDF文件。

电子。 G:

  $ HTTP({
       方法:POST,
       网址:'theUrl',
       数据://一些阵列接收
       标题://内容类型信息
}
.success(功能(响应){//我想保存响应为PDF});


解决方案

使用HTML5 FileSaver接口,可以做到这一点:

https://github.com/eligrey/FileSaver.js/

例解:

  //调用API使用POST请求来检索文件流
              $ http.post(URL,searchData,{responseTyp的:arraybuffer'}),然后(
                            响应=> {
                                从响应//下载文件
                                saveFileAs(响应);
                            },
                            数据=> {
                                //引发错误
                            }
                        );
             功能saveFileAs(响应){
                        VAR contentDisposition = response.headers(内容处置);
                        //从内容处置的文件名
                        变种文件名= contentDisposition.substr(contentDisposition.indexOf(文件名=)+ 9);
                        文件名= fileName.replace(/ \\/ g的,);
                        VAR的contentType = response.headers(内容类型);
                        VAR BLOB =新的Blob([response.data] {类型:contentType中});
                        的saveAs(BLOB,文件名);
                    }

I was wondering how I can save a file that is contained in a response from the server in angular ? (So that the file is automatically downloaded when the response arrives)

Edit :

I have a $http post method, and I get pdf data in the response. On success, I want to save the response data as a pdf file.

E. g :

$http({
       method: 'POST',
       url : 'theUrl',
       data: //some array that is received
       headers : //content type info
}
.success(function(response) { // I want to save the response as a pdf });

解决方案

Using HTML5 FileSaver interface, this can be achieved:

https://github.com/eligrey/FileSaver.js/

Example solution:

     //Call API to retrieve file stream using POST request 
              $http.post("URL", searchData, { responseType: 'arraybuffer' }).then(
                            response => {
                                //Download file from response
                                saveFileAs(response);
                            },
                            data => {
                                //raise error
                            }
                        );
             function saveFileAs(response) {
                        var contentDisposition = response.headers("content-disposition");
                        //Retrieve file name from content-disposition 
                        var fileName = contentDisposition.substr(contentDisposition.indexOf("filename=") + 9);
                        fileName = fileName.replace(/\"/g, "");
                        var contentType = response.headers("content-type");
                        var blob = new Blob([response.data], { type: contentType });
                        saveAs(blob, fileName);
                    }

这篇关于从HTTP响应保存在角文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆