Skip to content

Instantly share code, notes, and snippets.

@nobita4176
Last active May 31, 2017 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobita4176/60d4c6838015e983a337366fd8254293 to your computer and use it in GitHub Desktop.
Save nobita4176/60d4c6838015e983a337366fd8254293 to your computer and use it in GitHub Desktop.
var csv_parse = function(text) {
return text.split(/[\r\n]+/)
.map(function(l) {
var data = l.match(/("(?:[^"]|"")+"|[^,]+)/g);
if (data === null) {return null;}
return data.map(function(e) {
return e.replace(/^"(.*)"$/, '$1').replace('""', '"');
});
})
.filter(function(r) {return r !== null;});
};
$(document).on('change', '#input_file', function() {
if (this.files.length > 0) {
var file = this.files[0];
if (/\.csv$/.test(file.name)) {
var reader = new FileReader();
reader.addEventListener('load', function(ev) {
var data = csv_parse(ev.target.result);
/* do something */
});
reader.addEventListener('error', function(ev) {
console.error(ev.target.error);
});
reader.readAsText(file);
} else {
console.error('ファイルタイプが適正ではありません.');
return;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment