-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_select.html
54 lines (40 loc) · 1.62 KB
/
file_select.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
//check if browser supports file api and filereader features
if (window.File && window.FileReader && window.FileList && window.Blob) {
//this is not completely neccesary, just a nice function I found to make the file size format friendlier
//http://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable
function humanFileSize(bytes, si) {
var thresh = si ? 1000 : 1024;
if(bytes < thresh) return bytes + ' B';
var units = si ? ['kB','MB','GB','TB','PB','EB','ZB','YB'] : ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while(bytes >= thresh);
return bytes.toFixed(1)+' '+units[u];
}
//this function is called when the input loads an image
function renderImage(file){
var reader = new FileReader();
reader.onload = function(event){
the_url = event.target.result
//of course using a template library like handlebars.js is a better solution than just inserting a string
$('#preview').html("<img src='"+the_url+"' />")
$('#name').html(file.name)
$('#size').html(humanFileSize(file.size, "MB"))
$('#type').html(file.type)
}
//when the file is read it triggers the onload event above.
reader.readAsDataURL(file);
}
//watch for change on the
$( "#the-photo-file-field" ).change(function() {
console.log("photo file has been chosen")
//grab the first image in the fileList
//in this example we are only loading one file.
console.log(this.files[0].size)
renderImage(this.files[0])
});
} else {
alert('The File APIs are not fully supported in this browser.');
}