diff --git a/dist/JSONToCSVConvertor.js b/dist/JSONToCSVConvertor.js index 3fce1b6..2dc1efb 100644 --- a/dist/JSONToCSVConvertor.js +++ b/dist/JSONToCSVConvertor.js @@ -51,34 +51,26 @@ function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) { //Generate a file name var fileName = ""; //this will remove the blank-spaces from the title and replace it with an underscore - fileName += ReportTitle.replace(/ /g, "_"); - + fileName += ReportTitle.replace(/ /g, "_") + ".csv"; + //Initialize file format you want csv or xls - var uri = 'data:text/csv;charset=utf-8,' + escape(CSV); - + // Set UTF-8 formatting + var uri = "\uFEFF" + CSV; + // Now the little tricky part. // you can use either>> window.open(uri); // but this will not work in some browsers // or you will not get the correct file extension - - //this trick will generate a temp tag - - if (navigator.msSaveBlob) { - uri = 'data:text/csv;charset=utf-8,' + CSV; - navigator.msSaveBlob(new Blob([uri], {type: 'text/csv;charset=utf-8;'}), fileName + ".csv"); - } - - else { - var link = document.createElement("a"); - link.href = uri; - - //set the visibility hidden so it will not effect on your web-layout - link.style = "visibility:hidden"; - link.download = fileName + ".csv"; - - //this part will append the anchor tag and remove it after automatic click - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); + var blob = new Blob([uri], {type: 'text/csv;charset=utf-8;'}); + if (navigator.msSaveOrOpenBlob) { + navigator.msSaveBlob(blob, fileName); + } else { + //this trick will generate a temp tag + var elem = window.document.createElement('a'); + elem.href = window.URL.createObjectURL(blob); + elem.download = fileName; + document.body.appendChild(elem); + elem.click(); + document.body.removeChild(elem); } } \ No newline at end of file