Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for UTF-8 when downloading CSV #722

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"coverage:html": "cross-env NODE_ENV=test nyc check-coverage --lines 55 --reporter=html --reporter=text mocha --require babel-register test/*.js && nyc report --reporter=html",
"prettier": "find src/ docs/ test/ -type f -name \"*.js\" ! -path \"*/.next/*\" | xargs prettier --write",
"lint": "eslint src",
"build": "cross-env NODE_ENV=production npm run prettier && rollup -c",
"build": "cross-env NODE_ENV=production && rollup -c",
"prepare": "npm run build"
},
"repository": {
Expand Down
29 changes: 15 additions & 14 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,22 @@ function createCSVDownload(columns, data, options) {
const blob = new Blob([csv], { type: 'text/csv' });

/* taken from react-csv */
if (navigator && navigator.msSaveOrOpenBlob) {
navigator.msSaveOrOpenBlob(blob, options.downloadOptions.filename);
} else {
const dataURI = `data:text/csv;charset=utf-8,${csv}`;
// const dataURI = `data:text/csv;charset=utf-8,%EF%BB%BF${csv}`;

const URL = window.URL || window.webkitURL;
const downloadURI = typeof URL.createObjectURL === 'undefined' ? dataURI : URL.createObjectURL(blob);

let link = document.createElement('a');
link.setAttribute('href', downloadURI);
link.setAttribute('download', options.downloadOptions.filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
let CSVLink = document.createElement("a");
CSVLink.href = "data:text/csv;charset=utf-8,%EF%BB%BF" + csv;
CSVLink.target = "_blank";
CSVLink.download = options.downloadOptions.filename;
CSVLink.click()
// const URL = window.URL || window.webkitURL;
// const downloadURI = typeof URL.createObjectURL === 'undefined' ? dataURI : URL.createObjectURL(blob);
// alert("TEST")
// let link = document.createElement('a');
// link.setAttribute('href', downloadURI);
// link.setAttribute('download', options.downloadOptions.filename);
// document.body.appendChild(link);
// link.click();
// document.body.removeChild(link);
}

export { buildMap, getCollatorComparator, sortCompare, createCSVDownload };