Struggled a bit with encoding, escaping and headers.
Solution below:
JS:
columnDataToCSV : function(id) {
var results = typeof columnData[id].results != 'object' ? JSON.parse(columnData[id].results) : columnData[id].results;
var str = '';
/*
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Event type | Content | Date | Time | Notes | Tags | fromUser | toUser |
Tweet | I'm eating eggs! | 01/04/2013 | 18:01:12 | None | 123764, 41235 | spode | mled |
*/
// Headers
str += '"Event type","Content","Date","Time","Notes","Tags","fromUser","toUser"\r\n';
// Loop through column events
for (var i = 0; i < results.length; i++) {
var line = '';
// Event type
// line += '"' ++ '",';
if(results[i].eventType){
line += '"' +results[i].eventType+ '",';
} else {
line += ',';
}
// Content
if(results[i].text){
line += '"' +results[i].text+ '",';
} else {
line += ',';
}
// Date
if(results[i].occuranceTime){
// Date
line += '="' +results[i].occuranceTime.split(" ")[0]+ '",';
// Time
line += '="' +results[i].occuranceTime.split(" ")[1]+ '",';
} else {
line += ',';
}
// Notes
if(results[i].notes){
line += '"' +decodeURIComponent(results[i].notes[0].note)+ '",';
} else {
line += ',';
}
// Tags
if(results[i].Tags){
line += '"';
for(var x=0; x<results[i].Tags.length; x++){
line += Robin.data.tags[results[i].Tags[x].tagId].name + (x==results[i].Tags.length-1 ? '' : ', ' );
}
line += '",';
} else {
line += ',';
}
// fromUser
if(results[i].fromUser){
line += '"' +results[i].fromUser+ '",';
} else {
line += ',';
}
// toUser
if(results[i].toUser){
line += '"' +results[i].toUser+ '",';
} else {
line += ',';
}
//logD(line);
// Remove the last comma
line.slice(0,line.Length-1);
str += line + '\r\n';
}
return str;
},
exportToCSV: function(id){
var name = "myCSVFile";
var csvString = encodeURIComponent(columnDataToCSV(id));
var form = $('<form />');
form.addClass('hidden');
form.appendTo('body');
form.attr('name','export_'+id);
form.attr('action','/exporttocsv.php');
form.attr('method','post');
$('<input />').attr('name','filename').val(name).appendTo(form);
$('<input />').attr('name','csvdata').val(csvString).appendTo(form);
form.submit();
form.remove();
},
PHP
<?php
$csvdata = $_REQUEST['csvdata'];
$filename = $_REQUEST['filename'];
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
function utf8_urldecode($str) {
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;", urldecode($str));
return html_entity_decode($str,null,'UTF-8');
}
echo utf8_urldecode($csvdata);
?>
Solution below:
JS:
columnDataToCSV : function(id) {
var results = typeof columnData[id].results != 'object' ? JSON.parse(columnData[id].results) : columnData[id].results;
var str = '';
/*
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Event type | Content | Date | Time | Notes | Tags | fromUser | toUser |
Tweet | I'm eating eggs! | 01/04/2013 | 18:01:12 | None | 123764, 41235 | spode | mled |
*/
// Headers
str += '"Event type","Content","Date","Time","Notes","Tags","fromUser","toUser"\r\n';
// Loop through column events
for (var i = 0; i < results.length; i++) {
var line = '';
// Event type
// line += '"' ++ '",';
if(results[i].eventType){
line += '"' +results[i].eventType+ '",';
} else {
line += ',';
}
// Content
if(results[i].text){
line += '"' +results[i].text+ '",';
} else {
line += ',';
}
// Date
if(results[i].occuranceTime){
// Date
line += '="' +results[i].occuranceTime.split(" ")[0]+ '",';
// Time
line += '="' +results[i].occuranceTime.split(" ")[1]+ '",';
} else {
line += ',';
}
// Notes
if(results[i].notes){
line += '"' +decodeURIComponent(results[i].notes[0].note)+ '",';
} else {
line += ',';
}
// Tags
if(results[i].Tags){
line += '"';
for(var x=0; x<results[i].Tags.length; x++){
line += Robin.data.tags[results[i].Tags[x].tagId].name + (x==results[i].Tags.length-1 ? '' : ', ' );
}
line += '",';
} else {
line += ',';
}
// fromUser
if(results[i].fromUser){
line += '"' +results[i].fromUser+ '",';
} else {
line += ',';
}
// toUser
if(results[i].toUser){
line += '"' +results[i].toUser+ '",';
} else {
line += ',';
}
//logD(line);
// Remove the last comma
line.slice(0,line.Length-1);
str += line + '\r\n';
}
return str;
},
exportToCSV: function(id){
var name = "myCSVFile";
var csvString = encodeURIComponent(columnDataToCSV(id));
var form = $('<form />');
form.addClass('hidden');
form.appendTo('body');
form.attr('name','export_'+id);
form.attr('action','/exporttocsv.php');
form.attr('method','post');
$('<input />').attr('name','filename').val(name).appendTo(form);
$('<input />').attr('name','csvdata').val(csvString).appendTo(form);
form.submit();
form.remove();
},
PHP
<?php
$csvdata = $_REQUEST['csvdata'];
$filename = $_REQUEST['filename'];
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
function utf8_urldecode($str) {
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;", urldecode($str));
return html_entity_decode($str,null,'UTF-8');
}
echo utf8_urldecode($csvdata);
?>
No comments:
Post a Comment