forked from bgrins/filereader.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
450 lines (405 loc) · 13.4 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="demo/bootstrap.css" />
<style type='text/css'>
html, body { background-color: #eee; }
body { padding-top: 15px; background: url(demo/light_toast.png); }
#sidebar
{
left:auto;
right:20px;
width:260px;
position:fixed;
min-height: 40px;
overflow-y: auto;
background: white;
top: -10px;
bottom: -10px;
margin: 0;
border: solid 2px #333;
box-shadow: 0 0 10px #999;
}
#sidebar.drag
{
background: #f9b660;
}
.offset1 {
margin-left: 60px;
}
.offset2 {
margin-left: 80px;
}
.container-fluid > .content{ margin-right:340px; margin-left:0;}
#dropzone { padding:20px; border: solid 3px #050; text-align: center;}
#dropzone.drag { border: solid 3px #0f0; }
#file-list + small { display:none; }
#file-list:not(:empty) + small { display:block; }
#file-list:empty:before { content: "Add a file using one of the methods to the left and it will show up here."; display:block; }
#file-list { margin: 2px; }
#file-list li { margin:10px 0; list-style: none; }
#file-list li > img { max-width: 30px; max-height: 20px; margin-right: 3px; }
#file-list li .modal img { max-width: 400px; max-height: 400px; }
#file-list li.error { color: red; }
#file-list li.error a.btn { display:none; }
#file-list .group { font-size:1.1em; color:#666; border-bottom: dashed 1px #999; }
#file-list pre { margin:20px 0; border: solid 2px #8d8; display:none; }
#file-list pre.done { border-color: #8d8; }
.arrow_box {
position: relative;
background: #ffffff;
border: 2px solid #333;
}
.arrow_box:after, .arrow_box:before {
left: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
.arrow_box:after {
border-left-color: #ffffff;
border-width: 30px;
top: 50%;
margin-top: -30px;
}
.arrow_box:before {
border-left-color: #333;
border-width: 33px;
top: 50%;
margin-top: -33px;
}
.row p {
margin-bottom: 12px;
}
.row h2 {
margin: 10px 0;
}
#demo {
list-style-type: none;
border-radius: 5px;
margin-right: -7px;
padding: 15px;
}
#sync + strong {
color: #dec857;
}
#async + strong {
color: #7bde57;
}
body:after {
content: "";
display:block;
position:fixed;
top: -50px;
padding: 2px;
border-radius: 50%;
right:-50px;
width: 100px;
height: 100px;
background: #7bde57;
}
body.sync:after {
background: #dec857;
}
.done .filename { color: #357048; }
.done .not-done { display:none; }
.on-done { display:none; }
.done .on-done { display: inline-block; }
</style>
</head>
<body>
<div class="container-fluid">
<div class="content">
<div class="page-header">
<h1><a href='http://bgrins.github.com/filereader.js/'>FileReader.js</a> <small> - Read Files with JavaScript!</small></h1>
</div>
<div class="row">
<div class="offset1">
<h2>Read Files In The Browser</h2>
<p>
FileReader.js is intended to make accessing files via the <a href='http://dev.w3.org/2006/webapi/FileAPI/#FileReader-interface'>FileReader interface</a> easier.
Read more about <a href='http://www.briangrinstead.com/blog/filereaderjs'>the motivation behind filereader.js</a>.
</p>
</div>
</div>
<div class="row">
<div class="offset1">
<h2>No Dependancies</h2>
<p>
FileReader.js does not require any other libraries. <strong>If</strong> you do use jQuery, there is a small plugin wrapping up the functionlity though!
</p>
</div>
</div>
<div class="row">
<div class="offset1">
<h2>Source Code</h2>
<p>
<a class='btn btn-large btn-primary' href='http://github.com/bgrins/filereader.js'>View Project On Github</a>
</p>
<p>
Or you can jump straight to the
<a href='https://raw.github.com/bgrins/filereader.js/master/filereader.js'>filereader.js</a> or
<a href='https://raw.github.com/bgrins/filereader.js/master/filereader.min.js'>filereader.min.js</a>
if you are in a hurry.
</p>
<p>
If you've got some time to look around, check out the <a href='docs/filereader.html' target="_blank">annotated source code</a>!
</p>
</div>
</div>
<div class="row">
<div class="offset1">
<h2>Demonstration</h2>
<p>There are <strong>three ways</strong> to accept input - you can pick and choose any of these.</p>
</div>
</div>
<div class="row">
<ul id='demo' class='offset2 arrow_box'>
<li class=''>
<h3>File Input</h3>
<p>You can access files that users select via an input[type=file]. Try it out below:</p>
<form id='file-form' method="post" enctype="multipart/form-data">
<input type="hidden" name="extra-data" multiple />
<input type="file" id="file-input" name="file-input" multiple />
</form>
</li>
<li class="">
<h3>Drop Zone</h3>
<p id='dropzone'>
Drag and drop files from your desktop here (or select them from the input above).<br />
They will be read in browser.<br />
<img src='demo/arrow.png' width='50' /><br />
</p>
</li>
<li class="">
<h3>Clipboard</h3>
<p>
In some browsers, you can paste images onto a page. Go ahead and 'print screen' then press Ctrl+V to try that out!
</p>
</li>
</ul>
</div>
<div class="row">
<div class="offset1">
<h2>Usage</h2>
<p>
<strong>There is no jQuery or any other library dependancy for FileReader.js</strong>. It will work with a simple call to
<pre class="prettyprint">
<script type='text/javascript' src='filereader.js'></script>
</pre>
<pre class="prettyprint">
var opts = {
dragClass: "drag",
accept: false,
readAsMap: {
'image/*': 'DataURL',
'text/*' : 'Text'
},
readAsDefault: 'BinaryString',
on: {,
beforestart: function(e, file) {
// return false if you want to skip this file
},
loadstart: function(e, file) {
// Native ProgressEvent
},
progress: function(e, file) {
// Native ProgressEvent
},
load: function(e, file) {
// Native ProgressEvent
},
error: function(e, file) {
// Native ProgressEvent
},
loadend: function(e, file) {
// Native ProgressEvent
},
abort: function(e, file) {
// Native ProgressEvent
},
skip: function(e, file) {
// Called when a file is skipped. This happens when:
// 1) A file doesn't match the accept option
// 2) false is returned in the beforestart callback
},
groupstart: function(group) {
},
groupend: function(group) {
}
}
};
</pre>
<pre class="prettyprint">
FileReaderJS.setupInput(document.getElementById('file-input'), opts);
FileReaderJS.setupDrop(document.getElementById('dropzone'), opts);
FileReaderJS.setupClipboard(document.body, opts);
</pre>
</p>
<p>
If you do use jQuery, you could use it as such:
</p>
<pre class="prettyprint">
$("#file-input, #dropzone").fileReaderJS(opts);
$("body").fileClipboard(opts);
</pre>
</div>
</div>
<div class="row">
<div class="offset1">
<h2>Browser Support</h2>
<p>
<ul>
<li>Internet Explorer: <span class='label label-success'>10+</span></li>
<li>Firefox: <span class='label label-success'>10+</span></li>
<li>Chrome: <span class='label label-success'>13+</span></li>
<li>Opera: <span class='label label-success'>12+</span></li>
<li>Safari: <span class='label label-warning'>partial</span></li>
</ul>
For more information, see the <a href='http://caniuse.com/#feat=fileapi'>Full Browser Support Graph for File API</a>
</p>
</div>
</div>
<div class="row">
<div class="offset1">
<h2>Samples</h2>
<p>
Some sites have already implemented FileReader.js - check out their code to get an idea of how it is being used!
<ul>
<li>
<a href='http://mothereffinganimatedgif.com/'>mothereffinganimatedgif</a> - <a href='https://github.com/h5bp/mothereffinganimatedgif/'>source code</a>.
</li>
<li>
<a href='http://instantsprite.com/'>Instant Sprite CSS Sprite Generator</a> - <a href='https://github.com/bgrins/InstantSprite'>source code</a>.
</li>
</ul>
</p>
</div>
</div>
<div class="row">
<div class="offset1">
<h2>Resources</h2>
<p>
I found a lot of information while implementing this plugin. Here are some resources that helped:
<ul>
<li>
<a href='http://www.w3.org/TR/FileAPI/#dfn-filereader'>FileReader specification</a>
</li>
<li>
<a href='http://www.w3.org/TR/FileAPI/#FileReaderSync'>FileReaderSync specification</a>
</li>
<li>
<a href='ttp://www.w3.org/TR/FileAPI/#event-handler-attributes-section'>File API Event Handler specification</a>
</li>
<li>
<a href='http://caniuse.com/#feat=fileapi'>Full Browser Support Graph for File API</a>
</li>
</ul>
</p>
</div>
</div>
</div>
<div class="sidebar well" id="sidebar">
<h4>Demo - Loaded Files </h4>
<hr />
<p>
<form class='form'>
<fieldset>
<div class='controls'>
<label class="radio"><input type='radio' name='load-type' id='sync' /> <strong>Sync</strong> - Using <a target='_blank' href='http://www.w3.org/TR/FileAPI/#FileReaderSync'>FileReaderSync</a></label>
<label class="radio"><input type='radio' name='load-type' id='async' checked='checked' /> <strong>Async</strong> - Using FileReader</label>
</div>
</fieldset>
</form>
</p>
<hr />
<ul id='file-list'></ul>
<small><a href='#' id='clear-list'>clear all items</a></small>
</div>
<footer>
<p></p>
</footer>
</div>
<script src="demo/jquery-1.7.1.js"></script>
<script type='text/javascript' src='filereader.js'></script>
<script src="demo/bootstrap.js"></script>
<script type='text/javascript'>
var filelist = document.getElementById("file-list");
function groupTemplate(groupID, files) {
var html = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
var id = "group_" + groupID + "_file_" + file.extra.fileID;
html.push(
"<li id='" + id + "' data-fileid='" + file.extra.fileID + "' data-groupid='"+ groupID +"'>" +
"<span class='filename'>" + file.name + "</span> " +
"<div><span class='not-done'><em>Loading...</em></span><span class='on-done'><span class='time-to-load'></span> ms</span></div>" +
"<span class='details'><a href='#' class='btn'>details</a></span> " +
"<div class='modal hide'>" + file.name + "<br />" + file.type + "<br /></div>" +
"<pre>" + JSON.stringify(file, null, '\t') + "</pre>" +
"</li>"
);
}
var start = "<li id='group_" + groupID + "' class='group'>Group: " + groupID + " (" + files.length + " files) </li>";
return start + html.join('');
}
var opts = {
on: {
load: function(e, file) {
var fileDiv = $("#group_" + file.extra.groupID + "_file_" + file.extra.fileID)
fileDiv.addClass("done");
var ms = file.extra.ended - file.extra.started;
fileDiv.find(".time-to-load").text(ms);
if (file.type.match(/image/)) {
// Create a thumbnail and add it to the output if it is an image
var img = new Image();
img.onload = function() {
fileDiv.find(".modal").append(img);
fileDiv.prepend($(img).clone());
};
img.src = e.target.result;
}
else {
//fileDiv.find(".modal").append($("<div />").text(e.target.result));
}
},
error: function(e, file) {
$("#group_" + file.extra.groupID + "_file_" + file.extra.fileID).addClass("error");
},
groupstart: function(group) {
$(filelist).append(groupTemplate(group.groupID, group.files));
$(filelist).find(".details a:not(.initialized)").click(function() {
$(this).closest("li").find(".modal").modal('show');
})
$(filelist).find(".modal:not(.initialized)").addClass("initialized").modal({ show: false }).on("show", function() {
});
},
groupend: function(group) {
$("#group_" + group.groupID).append(
"<div>(Time to load: " + (group.ended - group.started) + "ms)</div>"
);
}
}
};
FileReaderJS.setupInput(document.getElementById('file-input'), opts);
FileReaderJS.setupDrop(document.getElementById('dropzone'), opts);
FileReaderJS.setupDrop(document.getElementById('sidebar'), opts);
FileReaderJS.setupClipboard(document.body, opts);
$(function() {
$(".sidebar").on('click', 'input', function() {
FileReaderJS.sync = $("#sync").is(":checked");
$("body").toggleClass("sync", !!FileReaderJS.sync);
});
$("#clear-list").on("click", function() {
$("#file-list").empty();
return false;
});
$("body").toggleClass("disabled", !FileReaderJS.enabled);
});
</script>
</body>
</html>