Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var data = [['name1', 'city1', 'some other info'], ['name2', 'city2', 'more info']];
- var csvContent = '';
- data.forEach(function (infoArray, index) {
- dataString = infoArray.join(';');
- csvContent += index < data.length ? dataString + '\n' : dataString;
- });
- var download = function(content, fileName, mimeType) {
- var a = document.createElement('a');
- mimeType = mimeType || 'application/octet-stream';
- if (navigator.msSaveBlob) { // IE10
- return navigator.msSaveBlob(new Blob([content], { type: mimeType }), fileName);
- } else if ('download' in a) { //html5 A[download]
- a.href = 'data:' + mimeType + ',' + encodeURIComponent(content);
- a.setAttribute('download', fileName);
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- return true;
- } else { //do iframe dataURL download (old ch+FF):
- var f = document.createElement('iframe');
- document.body.appendChild(f);
- f.src = 'data:' + mimeType + ',' + encodeURIComponent(content);
- setTimeout(function() {
- document.body.removeChild(f);
- }, 333);
- return true;
- }
- }
- download(csvContent, 'csv file.csv', 'text/csv');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement