Skip to content

Commit b57f69a

Browse files
committed
Update README.md
1 parent 38f1bc2 commit b57f69a

1 file changed

Lines changed: 31 additions & 57 deletions

File tree

README.md

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -34,69 +34,43 @@ console.log(sum(2)(3)); // Outputs 5
3434
```html
3535
<!DOCTYPE html>
3636
<html>
37-
<head>
38-
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
39-
<title>Show File Data</title>
40-
<script type="text/javascript">
41-
function showFileSize() {
42-
var input, file, extension;
43-
44-
// (Can't use `typeof FileReader === "function"` because apparently
45-
// it comes back as "object" on some browsers. So just see if it's there
46-
// at all.)
47-
if (!window.FileReader) {
48-
bodyAppend("p", "The file API isn't supported on this browser yet.");
49-
return;
50-
}
5137

52-
input = document.getElementById("fileinput");
53-
if (!input) {
54-
bodyAppend("p", "Um, couldn't find the fileinput element.");
55-
} else if (!input.files) {
56-
bodyAppend(
57-
"p",
58-
"This browser doesn't seem to support the `files` property of file inputs."
59-
);
60-
} else if (!input.files[0]) {
61-
bodyAppend("p", "Please select a file before clicking 'Load'");
62-
} else {
63-
file = input.files[0];
64-
extension = file.name.substring(file.name.lastIndexOf(".") + 1);
65-
bodyAppend(
66-
"p",
67-
"File Name: " +
68-
file.name +
69-
"<br/>File Size: " +
70-
file.size +
71-
" bytes <br/>File Extension: " +
72-
extension
73-
);
74-
}
75-
}
38+
<head>
39+
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
40+
<title>Show File Data</title>
41+
<script type="text/javascript">
42+
function showFileSize() {
43+
var input, file, extension;
7644
77-
function bodyAppend(tagName, innerHTML) {
78-
var elm;
45+
input = document.getElementById("fileinput");
7946
80-
elm = document.createElement(tagName);
81-
elm.innerHTML = innerHTML;
82-
document.body.appendChild(elm);
83-
}
84-
</script>
85-
</head>
86-
<body>
87-
<form action="#" onsubmit="return false;">
88-
<input type="file" id="fileinput" />
89-
<input
90-
type="button"
91-
id="btnLoad"
92-
value="Load"
93-
onclick="showFileSize();"
94-
/>
95-
</form>
96-
</body>
47+
file = input.files[0];
48+
extension = file.name.substring(file.name.lastIndexOf(".") + 1);
49+
50+
console.log("File Name: " + file.name);
51+
console.log("File Size: " + file.size + " bytes");
52+
console.log("File Extension: " + extension);
53+
}
54+
</script>
55+
</head>
56+
57+
<body>
58+
<form action="#" onsubmit="return false;">
59+
<input type="file" id="fileinput" />
60+
<input type="button" id="btnLoad" value="Load" onclick="showFileSize();" />
61+
</form>
62+
</body>
9763
</html>
64+
65+
<!-- Output -->
66+
67+
File Name: pic.jpg
68+
File Size: 1159168 bytes
69+
File Extension: jpg
9870
```
9971

72+
**&#9885; [Try this example on CodeSandbox](https://codesandbox.io/s/js-cp-file-upload-fj17kh?file=/index.html)**
73+
10074
<div align="right">
10175
<b><a href="#">↥ back to top</a></b>
10276
</div>

0 commit comments

Comments
 (0)