Last active
November 5, 2017 10:01
-
-
Save aniruddhanath/0abab05f34742f9d3f74 to your computer and use it in GitHub Desktop.
Download some data on DOM as CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var csvDownload = function (payload) { | |
var header = '"f1-value", "f2-value", "f3-value", "f4-value"\r\n', | |
link = document.createElement('a'), | |
csv; | |
payload.forEach(function(doc) { | |
data += '"' + doc.f1 + '",'; | |
data += '"' + doc.f2 + '",'; | |
data += '"' + doc.f3 + '",'; | |
data += '"' + doc.f4 + '"\r\n'; | |
} | |
csv = header + data; | |
link.href = 'data:text/csv;charset=utf-8,' + escape(csv); | |
link.target = '_blank'; | |
link.download = 'download.csv'; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment