angularJS实现无刷新文件下载

$scope.getExcel = function () {
            $http.post("/production/statistics/export", {
                storeId: $scope.$parent.currStore.storeId,
                date: $scope.$parent.ledgerDate.getTime()
            }, {responseType: "blob"}).success(function (data) {
                var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
                var fileName = $scope.$parent.currStore.name + "_生产统计_" + $scope.$parent.ledgerDate.Format("yyyy-MM-dd");
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.download = fileName;
                a.href = URL.createObjectURL(blob);
                a.click();
            })
        }
重点在于客户端设置请求头
responseType: "blob"

并且服务端返回的是二进制数据流.

客户端接收后转换为指定文件格式的blob,最后创建blob对象的URL 把它放在A标签的href上 就会自动下载了

你可能感兴趣的:(HTML5)