Javascript 从服务器下载一个文件,并以angularjs给出文件名

Javascript 从服务器下载一个文件,并以angularjs给出文件名,javascript,angularjs,zip,Javascript,Angularjs,Zip,我正试图从我的服务器(SpringMVC控制器)下载一个zip文件。 这是我在angularjs(1.5)控制器中下载zip文件的代码 $http({ url: '/myurl', method: 'GET', headers: { 'Content-type': 'application/zip' }, responseType: 'ar

我正试图从我的服务器(SpringMVC控制器)下载一个zip文件。 这是我在angularjs(1.5)控制器中下载zip文件的代码

   $http({
            url: '/myurl',
            method: 'GET',
            headers: {
                'Content-type': 'application/zip'
            },
            responseType: 'arraybuffer'
        }).success(function (data,status,headers) {
            var blob = new Blob([data], {type: "application/zip"});
            var objectUrl = URL.createObjectURL(blob);                
            var file = headers('Content-Disposition');               

            window.open(objectUrl);

        });
上面的代码可以工作,但我需要给出响应头中的文件名。我从标题('Content-Disposition')中获得了文件名。如何使用此文件名下载文件?目前,它提供任何随机文件名

我尝试了下面的代码,它在chrome中工作,但在mozilla中不工作。。。是否有任何其他解决方案可以在所有浏览器中使用

            //var anchor = document.createElement("a");
            //anchor.download = "ATMOSLogFiles.zip";
            //anchor.href = objectUrl;
            //anchor.click();
谢谢你的帮助

基于blob的解决方案: 你可以用它来实现这一点

var app = angular.module('myApp', ['ngFileSaver'])

app.controller('ExampleCtrl', ['FileSaver', 'Blob', function () {
    $scope.download = function () {
        var myData = new Blob([text], { type: 'text/plain;charset=utf-8' });
        FileSaver.saveAs(myData, 'text.txt');
    }
}]);

另一个基于HTML5的解决方案: 使用HTML5/的简单方法。不需要水滴。任何支持AngularJS的浏览器和浏览器版本都支持此属性(IE10/IE11除外-IE Edge不支持此属性)


以上@lin的回答是正确的,但我想补充一点,根据问题的需要,可以直接将服务器上设置的文件名传递给客户端的文件名,如下所示:

只需安装,在应用程序中引用,并将其作为依赖项注入即可

    var app = angular.module('myApp', ['ngFileSaver']);
    app.controller('mainCtrl', ['FileSaver', 'Blob', '$http', '$scope', function(FileSaver, Blob, $http, $scope) {
              // make ajax call to server
      $scope.download = function() {
           var req = {
               url: 'your server url',
               method: 'GET',
               responseType: 'arraybuffer'
           };
         $http(req).then(function(resp){
                var serverData = new Blob([resp.data], {type: resp.headers()['content-type']});
                FileSaver.saveAs(serverData, resp.headers()['content-disposition']); // File name set at server passed to the FileSaver function
         });
      }
}]);

尝试搜索“spring附件”,为什么首先要使用Ajax?为什么要将请求头设置为
“Content-type”:“application/zip”
?这是一个GET请求。请求中没有正文描述的内容类型。有任何反馈或建议吗?是一种很好的填充物。
    var app = angular.module('myApp', ['ngFileSaver']);
    app.controller('mainCtrl', ['FileSaver', 'Blob', '$http', '$scope', function(FileSaver, Blob, $http, $scope) {
              // make ajax call to server
      $scope.download = function() {
           var req = {
               url: 'your server url',
               method: 'GET',
               responseType: 'arraybuffer'
           };
         $http(req).then(function(resp){
                var serverData = new Blob([resp.data], {type: resp.headers()['content-type']});
                FileSaver.saveAs(serverData, resp.headers()['content-disposition']); // File name set at server passed to the FileSaver function
         });
      }
}]);