-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.imagepreview.js
39 lines (31 loc) · 1.02 KB
/
jquery.imagepreview.js
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
(function($){
$.fn.imagePreview = function(params){
$(this).change(function(evt){
if(typeof FileReader == "undefined") return true; // File reader not available.
var fileInput = $(this);
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var imgHTML = '<img class="file-input-thumb" src="' + e.target.result + '" title="' + theFile.name + '"/>';
if( typeof params.selector != 'undefined' ){
$(params.selector).html(imgHTML);
}else{
fileInput.before(imgHTML);
}
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
});
};
})(jQuery);