Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created May 10, 2014 02:53
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 joyrexus/a8c92a2779029208c677 to your computer and use it in GitHub Desktop.
Save joyrexus/a8c92a2779029208c677 to your computer and use it in GitHub Desktop.
Load a file

Load a TSV-file using the File API.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
margin-top: 25px;
margin-left: 50px;
font-family: "Helvetica Neue", sans-serif;
font-weight: 100;
font-size: 60px;
color: #777;
-webkit-font-smoothing: antialiased;
}
#file {
color: steelblue;
font-size: 60px;
}
#file:hover {
color: orange;
cursor: pointer;
}
input {
visibility: hidden;
}
.info {
display: flex;
font-size: 25px;
width: 515px;
}
</style>
<body>
<div id="fileInfo">
<div id="file">Click and choose a file</div>
<input type="file" id="chooser">
</div>
<div id="data"></div>
<script src="main.js"></script>
window.rows = []
# load handler invoked on change event
load = ->
File = @files[0]
return if not File.name.match '\.tsv$'
file.textContent = File.name
reader = new FileReader()
reader.onload = (file) ->
@result.split('\n').map (row) ->
window.rows.push(row.split('\t')) if row.match /^\d/
data.textContent = @result
reader.readAsText File
# click and choose a file to load
chooser.addEventListener('change', load)
file.addEventListener('click', -> chooser.click())
(function() {
var load;
window.rows = [];
load = function() {
var File, reader;
File = this.files[0];
if (!File.name.match('\.tsv$')) {
return;
}
file.textContent = File.name;
reader = new FileReader();
reader.onload = function(file) {
this.result.split('\n').map(function(row) {
if (row.match(/^\d/)) {
window.rows.push(row.split('\t'));
}
});
data.textContent = this.result;
};
reader.readAsText(File);
};
chooser.addEventListener('change', load);
file.addEventListener('click', function() {
chooser.click();
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment