AngularJS 下载文件流
时间: 2023-08-16 17:45:22 浏览: 104
在AngularJS中,可以使用$http服务来发送HTTP请求,从而下载文件流。具体步骤如下:
1. 在控制器中注入$http服务。
```
app.controller('myCtrl', function($scope, $http) {
// code here
});
```
2. 使用$http服务发送GET请求,设置responseType为'arraybuffer',并在成功回调函数中处理返回的文件流。
```
$http({
method: 'GET',
url: 'yourUrl',
responseType: 'arraybuffer'
}).then(function(response) {
var blob = new Blob([response.data], {type: 'application/octet-stream'});
var fileName = 'yourFileName';
saveAs(blob, fileName);
});
```
3. 在成功回调函数中,将返回的二进制数据转换成Blob对象,并使用第三方库FileSaver.js中的saveAs函数将Blob对象下载为文件。需要注意的是,FileSaver.js需要单独引入。
以上代码中,saveAs函数用于保存文件,需要在HTML页面中引入FileSaver.js库。
```
<script src="https://cdn.jsdelivr.net/g/filesaver.js"></script>
```
阅读全文
相关推荐
















