-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileReader.htm
68 lines (60 loc) · 2.06 KB
/
FileReader.htm
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
58
59
60
61
62
63
64
65
66
67
68
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FileReader</title>
</head>
<body onload="readTextFile2('./txt.txt');">
<div>
<h1>Text File Reader</h1>
<form>
Select a text file:
<input type="file" id="fileInput" onchange="readTextFile1();">
</form>
<BR><BR>
<fieldset>
<legend>File Display Area</legend>
<form id="fileDisplayArea">
</form>
</fieldset>
<br>
<div id="content" style="border: 2px black solid; padding: 4px;"></div>
</div>
<script>
var fileInput = document.getElementById('fileInput');
var fileDisplayArea = fileDisplayArea = document.getElementById('fileDisplayArea');
function readTextFile1() {
var file = fileInput.files[0];
var textType = /text.*/;
if (file.type.match(textType)) {
var reader = new FileReader();
reader.onload = function (e) {
fileDisplayArea.innerText = reader.result;
}
reader.readAsText(file);
} else {
fileDisplayArea.innerText = "File not supported!"
}
}
function readTextFile2(file) {
var rawFile;
if (navigator.appName.search('Microsoft') > -1) {
rawFile = new ActiveXObject('MSXML2.XMLHTTP');
} else {
rawFile = new XMLHttpRequest();
}
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
console.log(rawFile);
var el = document.getElementById('content');
el.innerHTML = rawFile.responseText;
}
}
}
rawFile.send(null);
}
</script>
</body>
</html>