From af32038f381180f9b77b490f2f3c61fe36f533fc Mon Sep 17 00:00:00 2001 From: Elizabeth Date: Mon, 23 Oct 2017 11:25:47 -0700 Subject: [PATCH 1/2] Fix network error on large CSV downloads in Chrome --- dist/JSONToCSVConvertor.js | 42 +++++++++++++++----------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/dist/JSONToCSVConvertor.js b/dist/JSONToCSVConvertor.js index 3fce1b6..0cd99b3 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, "_"); - - //Initialize file format you want csv or xls - var uri = 'data:text/csv;charset=utf-8,' + escape(CSV); - + fileName += ReportTitle.replace(/ /g, "_") + ".csv"; + + //Initialize file format you want csv or xls + // 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 From c3fdb80d5bb4ee3bf5730cab5c5e889a737fcb1e Mon Sep 17 00:00:00 2001 From: Elizabeth Date: Mon, 23 Oct 2017 11:29:14 -0700 Subject: [PATCH 2/2] Fix spacing --- dist/JSONToCSVConvertor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/JSONToCSVConvertor.js b/dist/JSONToCSVConvertor.js index 0cd99b3..2dc1efb 100644 --- a/dist/JSONToCSVConvertor.js +++ b/dist/JSONToCSVConvertor.js @@ -53,8 +53,8 @@ function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) { //this will remove the blank-spaces from the title and replace it with an underscore fileName += ReportTitle.replace(/ /g, "_") + ".csv"; - //Initialize file format you want csv or xls - // Set UTF-8 formatting + //Initialize file format you want csv or xls + // Set UTF-8 formatting var uri = "\uFEFF" + CSV; // Now the little tricky part.