Last active
March 6, 2018 01:31
-
-
Save jiyu3/a716a49cc600f048293c081944c13930 to your computer and use it in GitHub Desktop.
Javascriptで巨大なテキストファイルを少しずつ読み込む ref: https://qiita.com/jiyu/items/3e29959758de497850b9
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
readBigTextForEachLine = (file, callback, chunk_size = 1024) => { | |
let offset = 0; | |
let text = ""; | |
let slice = ""; | |
let fr = new FileReader(); | |
fr.onload = (event) => { | |
if(typeof fr.result === "string") { | |
callback(text + fr.result.replace(/\r/g, "\n")); | |
return true; | |
} | |
let view = new Uint8Array(fr.result); | |
for(let i=0, l=view.length; i<l; i++) { | |
if(view[i] === 13) { // \n = 10 and \r = 13 | |
if(view[i+1] === 10) { | |
i++; | |
} | |
callback(text + "\n"); | |
text = ""; | |
continue; | |
} | |
text += String.fromCharCode(view[i]); | |
} | |
seek(); | |
}; | |
fr.onerror = () => { | |
callback("Failed to read file."); | |
}; | |
let seek = () => { | |
if (offset + chunk_size >= file.size) { | |
slice = file.slice(offset); | |
fr.readAsText(slice); | |
} else { | |
slice = file.slice(offset, offset + chunk_size); | |
fr.readAsArrayBuffer(slice); | |
} | |
offset += chunk_size; | |
} | |
seek(); | |
} |
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
<script> | |
document.getElementById("upload").onchange = (e) => { | |
let file = e.target.files[0]; | |
readBigTextForEachLine(file, console.log); | |
}; | |
</script> | |
<html><body> | |
Upload Big File: <input type="file" id="upload"> | |
</body></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment