Last active
May 31, 2017 01:49
-
-
Save nobita4176/60d4c6838015e983a337366fd8254293 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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