-
Notifications
You must be signed in to change notification settings - Fork 0
/
filereader.js
57 lines (50 loc) · 1.6 KB
/
filereader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// code from bcmoney at Stackoverflow
// https://stackoverflow.com/questions/13709482/how-to-read-text-file-in-javascript
// [External dependencies]
// initialize: visual.js
var reader; //GLOBAL File Reader object
function checkFileAPI() {
if (window.File && window.FileReader && window.FileList && window.Blob) {
reader = new FileReader();
return true; }
else {
alert('The File APIs are not fully supported by your browser. Fallback required.');
return false; }
}
function readText(filePath) {
var output = "";
//placeholder for text output
if(filePath.files && filePath.files[0]) {
reader.onload = function (e) {
output = e.target.result;
j = JSON.parse(output);
initialize(window.j);
};//end onload()
reader.readAsText(filePath.files[0]);
}//end if html5 filelist support
else { return false; }
return true;
}
var parse_url = "/toys/visual_b_parse/"
// used for online service
function readAndUploadText(filePath){
//placeholder for text output
if(filePath.files && filePath.files[0]) {
reader.onload = function (e) {
console.log(e)
var text = e.target.result;
setCode("\n"+text);
$.post({
url: parse_url,
data: {'text':text},
success: function(result){
window.j = result['tree']
initialize(window.j);
}
})
};
reader.readAsText(filePath.files[0]);
}
else { return false; }
return true;
}