Skip to content

Commit b8f88cd

Browse files
committed
Added simple file api example
1 parent 3ba5802 commit b8f88cd

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

demos.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"url": "dataset",
55
"tags": "dataset",
66
"support": {
7-
"live": "chrome",
7+
"live": "chrome opera",
88
"nightly": "safari"
99
},
1010
"test": "'dataset' in document.createElement('i')"
@@ -20,6 +20,16 @@
2020
},
2121
"test": "Modernizr.history"
2222
},
23+
{
24+
"desc": "Browser based file reading",
25+
"url": "file-api-simple",
26+
"note": "Not part of HTML5",
27+
"tags": "file-api",
28+
"support": {
29+
"live": "firefox chrome opera"
30+
},
31+
"test": "typeof FileReader != 'undefined'"
32+
},
2333
{
2434
"desc": "Drag files directly into your browser",
2535
"url": "file-api",
@@ -28,7 +38,7 @@
2838
"support": {
2939
"live": "firefox chrome"
3040
},
31-
"test": "typeof FileReader != 'undefined'"
41+
"test": "typeof FileReader != 'undefined' && Modernizr.draganddrop"
3242
},
3343
{
3444
"desc": "Simple chat client",

demos/file-api-simple.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<title>File API (simple)</title>
2+
<article>
3+
<p id="status">File API &amp; FileReader API not supported</p>
4+
<p><input type=file></p>
5+
<p>Select an image from your machine to read the contents of the file without using a server</p>
6+
<div id="holder"></div>
7+
</article>
8+
<script>
9+
var upload = document.getElementsByTagName('input')[0],
10+
holder = document.getElementById('holder'),
11+
state = document.getElementById('status');
12+
13+
if (typeof window.FileReader === 'undefined') {
14+
state.className = 'fail';
15+
} else {
16+
state.className = 'success';
17+
state.innerHTML = 'File API & FileReader available';
18+
}
19+
20+
upload.onchange = function (e) {
21+
e.preventDefault();
22+
23+
var file = upload.files[0],
24+
reader = new FileReader();
25+
reader.onload = function (event) {
26+
var img = new Image();
27+
img.src = event.target.result;
28+
// note: no onload required since we've got the dataurl...I think! :)
29+
if (img.width > 560) { // holder width
30+
img.width = 560;
31+
}
32+
holder.innerHTML = '';
33+
holder.appendChild(img);
34+
};
35+
reader.readAsDataURL(file);
36+
37+
return false;
38+
};
39+
</script>

0 commit comments

Comments
 (0)