Open aiyubaarav opened 2 years ago
Is the output itself incorrect, or does Safari "render" the csv incorrectly. End of line characters are unfortunately not standardized. Are there multiple lines, if you save the generated file and open it in an editor?
What happens, if you change the eol character?
I am facing same issue for Firefox browser.
Steps to reproduce:
flutter run -d web-server
Then open the provided localhost link in Firefox browser. (Here we can also open the same link in Chrome to test the difference in output)
then onButton click convert the list to csv using plugin and download the file. code for this:
//This code is for converting
final csv1 = const ListToCsvConverter(eol: "\n", delimitAllFields: true).convert([
["ab", DateFormat("MM/dd/yyyy").format(Timestamp.now().toDate()).toString()],
["ab", DateFormat("MM/dd/yyyy").format(Timestamp.now().toDate()).toString()],
], eol: "\n", delimitAllFields: true);
//This code is for downloading the file
AnchorElement(href: "data:text/plain;charset=utf-8,$csv1")
..setAttribute("download", "TickerData${DateTime.now()}.csv")
..click();
I downloaded files from both Firefox and Chrome and open it on Microsoft excel. Both files seems to be slightly different size. Also, I have tried using eol: "\n"
and eol: "\t"
It seems to work on chrome while Firefox is a no go.
I opened both files in notepad and chrome file is showing the second entry in next line while Firefox file showing everything in one line.
Let me know if any workaround for this one.
Thanks
I'm seeing the same issue on Safari (fine on Chrome) - has anyone figured out a workaround?
I recently met this issue and here is a working example using blob:
import 'dart:convert';
import 'dart:html' as html;
import 'package:csv/csv.dart';
void downloadCSV(String fileName, List<List<String>> csvRows) {
final csvData = const ListToCsvConverter().convert(csvRows);
final bytes = utf8.encode(csvData);
final blob = html.Blob([bytes]);
final url = html.Url.createObjectUrlFromBlob(blob);
html.AnchorElement(href: url)
..download = fileName
..click();
}
I am using this library to generate the csv from array list in flutter web
When there is chrome works fine. But in safari it gives me the csv data but not in different raw but in all the records in single row
If anyone has solution please provide
Thanks in advance