javascript internet explorer byte order mark -
i working on way save html table csv file. ideally, should cross-browser, , have gotten work on internet explorer. however, have gotten obvious parts working. remains unable working csv file javascript because byte order mark prepended data wish download.
i have confirmed case downloading csv file in ie , else , used hex editor view raw file, can confirm file internet explorer downloaded prepends unicode character "fffe".
please see code below this. savetable takes "<a>" node located inside table.
if can me disgnose issue , offer solution i'd grateful. please forgive faux pas on part, don't think i've ever used site of nature before. if need me provide further information please let me know , shall best on here.
function findtable(node) { // finds nodes parent table. return (node.nodename !== "table") ? findtable(node.parentnode) : node; } function savetable(node) { var csv = []; var table = findtable(node); var rows = table.getelementsbytagname("tr"); var header = []; var csv = []; (var = 0; < rows.length; i++) { if (i == 0) { // csv stuff. var dates = rows[i].getelementsbytagname("th"); (var j = 0; j < dates.length; j++) (j == 0) ? header.push("") : header.push(dates[j].innerhtml); csv.push(header.join(",")); } else { var rowarray = []; var jobname = rows[i].getelementsbytagname("th")[0].innerhtml; var times = rows[i].getelementsbytagname("td"); rowarray.push(jobname); (var k = 0; k < times.length; k++) rowarray.push(times[k].innerhtml); csv.push(rowarray.join(",")); } } node.setattribute("href", "data:text/csv;charset=utf-8," + csv.join("%0a")); var filename = "spreadsheet_data-" + (new date).gettime() + ".csv"; if (node.download == "") node.setattribute("download", filename); else { alert("handle ie here!"); var bom = "\ufffe"; var doc = document.open("application/octet-stream", "_blank"); var data = csv.join("\r\n"); doc.charset = "utf-8"; doc.write(data.replace(bom, "")); doc.focus(); doc.execcommand('saveas', false, filename); doc.close(); } }
table example, it's not way have chosen myself, it's how table generated piece of software.
<table id='results' border='1'> <tr><th><a href='#' onclick='savetable(this);' id='download_link'>download data</a></th><th>2013/05/09</th><th>2013/05/10</th><th>2013/05/10</th><th>2013/05/10</th><th>2013/05/10</th></tr> <tr> <th>\pdf\exovign.pdf</th><td>8.853</td><td>9.050</td><td>8.807</td><td>8.827</td><td>8.835</td></tr> </table>
if have no absolute requirement client-side, might save lot of hassle send file server instead.
Comments
Post a Comment