javascript - Write to CSV file locally with HTML5 -
i wondering whether html5 allows 1 save/write local file within user's file system. i'm asking know html5 can export data client , download csv file example.
if not html5, best approach overwrite contents, or create csv (or other type of file matter) locally within client-side script?
function:
function exportdata() { var data = ''; (var i=1;i<=2;i++) { var sep = ''; (var j=1;j<=4;j++) { data += sep + document.getelementbyid(i + '_' + j).value; sep = ','; } data += '\r\n'; } var exportlink = document.createelement('a'); exportlink.setattribute('href', 'data:text/csv;base64,' + window.btoa(data)); exportlink.appendchild(document.createtextnode('test.csv')); document.getelementbyid('results').appendchild(exportlink); }
markup:
<input type="number" id="1_1" value="2">, <input type="number" id="1_2" value="1">, <input type="number" id="1_3" value="4">, <input type="number" id="1_4" value="3"> <br> <input type="number" id="2_1" value="1">, <input type="number" id="2_2" value="2">, <input type="number" id="2_3" value="3">, <input type="number" id="2_4" value="4"> <br> <button onclick="exportdata()">export csv</button> <div id="results"></div>
demo:
click button link, click link , file. change values, click link again , different file. firefox made me select excel every time open don't know whether that's configuration or general issue.
reference:
Comments
Post a Comment