使用AngularJS下载zip文件

Downloading zip file with AngularJS

本文关键字:文件 zip 下载 AngularJS 使用      更新时间:2023-09-26

当我通过浏览器访问此url时,它会要求我下载文件

https://some-domain.com/api/v1/file/log123.tar.gz?type=log

我需要从angularjs http请求下载这个

我为此写了一个工厂,但它没有下载

请求获得200状态,但仍显示firebug 中的加载程序

app.factory('File', ['$http','GENERAL_CONFIG', function($http, GENERAL_CONFIG) {
    var API_URL = GENERAL_CONFIG.BASE_API_URL;
    API_URL = API_URL + 'filetransfer/';
    return {
        download: function(filename, type,successcallback, errorcallback){
            var apiurl = API_URL + filename + '?type='+type
            $http.get(apiurl,{
                headers: {
                    "Content-Type": "application/x-download;"
                }
            }).success(successcallback).error(errorcallback);
        }
    }
}]);

这是一篇旧帖子,但我会根据当前需求做出回应。

您的http请求应该有一个额外的头。设置responseType:"arraybuffer"

然后,您可以将该响应转换为blob对象,并将其传递给回调:URL.createObjectURL(new blob([响应]));