[英]Angular download csv file on browser
I have Rest API that sends the csv String like 我有Rest API发送csv String
"aa,bb\n cc,dd"
Here is the Rest Service code: 这是休息服务代码:
@GET
@Path("/getCsv")
@Produces({"text/csv"})
public String getCSV( ) {
return "aa,ba,\n,ac,da";
}
On UI side 在UI方面
Angular resource code to make get request code is as below: 用于获取请求代码的Angular资源代码如下:
angular.module('csvHelper', ['ngResource','configuration']).factory('csvHelper', function($resource,$rootScope){
return $resource(URL+'/api/getCsv/', {}, {
query: {method:'GET', params:{}, cache :true}
});
});
Code that makes get request and generate csv is as follows 发出获取请求并生成csv的代码如下
csvHelper.query({}, function(response) {
def.resolve(response);
},function(response) {
def.reject(response);
});
var prom = def.promise;
prom.then(function(text) {
$window.open("data:text/csv;charset=utf-8," + encodeURIComponent(text));
}, function(error) {
console.log('promise failed', error);
});
In network tab I see rest API sending the data properly: 在网络标签中,我看到其他API正确发送了数据:
"aa,ba,\n,ac,da"
But when I download the csv file, inside it I see 但是当我下载csv文件时,在其中看到了
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What am I doing wrong. 我究竟做错了什么。
EDIT 编辑
I tired to print response on console and this is what I saw: 我厌倦了在控制台上打印响应,这是我看到的:
[e { 0="a", $get=function(), $save=function(), more...}, e { 0="a", $get=function(), $save=function(), more...}, e { 0=",", $get=function(), $save=function(), more...}, e { 0="b", $get=function(), $save=function(), more...}, e { 0="a", $get=function(), $save=function(), more...}, e { 0=",", $get=function(), $save=function(), more...}, e { 0="\n", $get=function(), $save=function(), more...}, e { 0=",", $get=function(), $save=function(), more...}, e { 0="a", $get=function(), $save=function(), more...}, e { 0="c", $get=function(), $save=function(), more...}, e { 0=",", $get=function(), $save=function(), more...}, e { 0="d", $get=function(), $save=function(), more...}, e { 0="a", $get=function(), $save=function(), more...}]
EDIT 2 编辑2
I changed the logic to use $http like this: 我更改了使用$ http的逻辑,如下所示:
$http.get("http://myurl.com/api/getcsv")
.success(function (response) {console.log(success)});
I see the success printed on console but I don't see popup. 我看到成功打印在控制台上,但没有看到弹出窗口。
when I hit http://myurl.com/api/getcsv in browser I see popup to download file. 当我在浏览器中点击http://myurl.com/api/getcsv时 ,我看到弹出窗口以下载文件。
What am I missing 我在想什么
EDIT 3 编辑3
This change worked. 此更改有效。
$http.get(compiledUrl).then(function (response) {
$window.open("data:text/csv;charset=utf-8," + encodeURIComponent(response.data));
},function(response){
console.log("error");
});
If you just want to download the CSV string, you should use $http and not $resource. 如果只想下载CSV字符串,则应使用$ http而不是$ resource。
$resource returns: A resource "class" object with methods for the default set of resource actions optionally extended with custom actions. $ resource返回:一个资源“类”对象,该对象具有用于默认资源操作集的方法,可以选择使用自定义操作进行扩展。
you can use two custom function 您可以使用两个自定义功能
1 1个
function JSONToCSVConvertor(JSONData,ShowLabel) {
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) :
SONData;
var CSV = '';
if (ShowLabel) {
var row = "";
for (var index in arrData[0]) {
row += index + ',';
}
row = row.slice(0, -1);
CSV += row + '\r\n';
}
for (var i = 0; i < arrData.length; i++) {
var row = "";
for (var index in arrData[i]) {
var arrValue = arrData[i][index] == null ? "" : '="' + arrData[i][index] + '"';
row += arrValue + ',';
}
row.slice(0, row.length - 1);
CSV += row + '\r\n';
}
if (CSV == '') {
growl.error("Invalid data");
return;
}
var fileName = "Result";
if(msieversion()){
var IEwindow = window.open();
IEwindow.document.write('sep=,\r\n' + CSV);
IEwindow.document.close();
IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
IEwindow.close();
} else {
var uri = 'data:application/csv;charset=utf-8,' + escape(CSV);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = fileName + ".csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
2 2
function msieversion() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie != -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
{
return true;
} else { // If another browser,
return false;
}
return false;
}
and use above function to have downloadable result.csv file 并使用上述功能下载result.csv文件
if(data.userlog == '')
return;
JSONToCSVConvertor(data.userlog, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.