下载文本/ CSV内容文件从服务器角度 [英] Download text/csv content as files from server in Angular
本文介绍了下载文本/ CSV内容文件从服务器角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想一个流 CSV
从的node.js服务器文件。服务器部分是很简单的:
I am trying to stream a csv
file from a node.js server. The server portion is very simple :
server.get('/orders' function(req, res) {
res.setHeader('content-type', 'text/csv');
res.setHeader('content-disposition', 'attachment; filename='orders.csv');
return orders.pipe(res); // assuming orders is a csv file readable stream (doesn't have to be a stream, can be a normal response)
}
在我的角度控制器我试图做这样的事情。
In my angular controller I am trying to do something like this
$scope.csv = function() {
$http({method: 'GET', url: '/orders'});
};
时,有上点击一个按钮,在我看来 NG-点击
这个函数被调用:
<button ng-click="csv()">.csv</button>
我已经看过关于角从服务器下载文件的其他答案,但没有发现任何东西为我工作。有没有做到这一点的常用方法?看来喜欢的事,应该很简单。
I have looked at other answers about downloading files from server in Angular, but didn't find anything that worked for me. Is there a common way to do this ? Seems like something that should be simple.
推荐答案
$ HTTP
服务返回诺
这有两个回调方法如下所示。
$http
service returns a promise
which has two callback methods as shown below.
$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
var anchor = angular.element('<a/>');
anchor.attr({
href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
target: '_blank',
download: 'filename.csv'
})[0].click();
}).
error(function(data, status, headers, config) {
// handle error
});
这篇关于下载文本/ CSV内容文件从服务器角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文