-
Notifications
You must be signed in to change notification settings - Fork 0
/
CW5.HTML
82 lines (61 loc) · 1.9 KB
/
CW5.HTML
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>Fetch Remote File </title>
</head>
<script>
"use strict";
let local = []
let remote;
function fileSelect(evt) {
local = []
var files = evt.files; // FileList object
for (let file of files) {
let reader = new FileReader();
reader.onload = function () {
// out.innerText +=reader.result +"\n";
local.push({ data: reader.result, name: file.name });
}
reader.readAsText(file);
}
}
function readText() {
fetch(url.value)
.then(r => r.text())
//response of fetch() is r
.then(t => remote = t).then(compare);
//response of text() is t
}
function compare() {
let text = "";
local.forEach(file => {
if(file.data == remote){
text = " "+file.name +" has the same content"
// console.log("equal")
}
}
)
out.innerText=""
out.innerText = local.length +" files reader"+ text;
}
</script>
<body>
<h2 id=title></h2>
<input type=text id=url value="https://maeyler.github.io/BLM305/README.md">
<!--onChange="readText()"-->
<!-- <input type="file" id="files" name="files[]" onChange="fileSelect()" multiple /> -->
<input type=file id=button onChange='fileSelect(this)' multiple />
<input type=button value="Compare" onClick="readText()">
<!-- <input type=button value="compare" onClick="compare()"> -->
<pre id=out></pre>
<hr />
<b>Sample code</b>
<pre id=sample></pre>
<hr />
<script>
title.innerText = document.title;
</script>
</body>
</html>