-
Notifications
You must be signed in to change notification settings - Fork 1
/
file-upload.js
129 lines (104 loc) · 4.43 KB
/
file-upload.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
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
//Reference:
//https://www.onextrapixel.com/2012/12/10/how-to-create-a-custom-file-input-with-jquery-css3-and-php/
;(function($) {
// Browser supports HTML5 multiple file?
var multipleSupport = typeof $('<input/>')[0].multiple !== 'undefined',
isIE = /msie/i.test( navigator.userAgent );
$.fn.customFile = function() {
return this.each(function() {
var $file = $(this).addClass('custom-file-upload-hidden'), // the original file input
$wrap = $('<div class="file-upload-wrapper">'),
$input = $('<input type="text" class="file-upload-input" />'),
// Button that will be used in non-IE browsers
$button = $('<button type="button" class="file-upload-button">Select a File</button>'),
// Hack for IE
$label = $('<label class="file-upload-button" for="'+ $file[0].id +'">Select a File</label>');
// Hide by shifting to the left so we
// can still trigger events
$file.css({
position: 'absolute',
left: '-9999px'
});
$wrap.insertAfter( $file )
.append( $file, $input, ( isIE ? $label : $button ) );
// Prevent focus
$file.attr('tabIndex', -1);
$button.attr('tabIndex', -1);
$button.click(function () {
$file.focus().click(); // Open dialog
});
$file.change(function() {
var files = [], fileArr, filename;
// If multiple is supported then extract
// all filenames from the file array
if ( multipleSupport ) {
fileArr = $file[0].files;
for ( var i = 0, len = fileArr.length; i < len; i++ ) {
files.push( fileArr[i].name );
}
filename = files.join(', ');
// If not supported then just take the value
// and remove the path to just show the filename
} else {
filename = $file.val().split('\\').pop();
}
$input.val( filename ) // Set the value
.attr('title', filename) // Show filename in title tootlip
.focus(); // Regain focus
});
$input.on({
blur: function() { $file.trigger('blur'); },
keydown: function( e ) {
if ( e.which === 13 ) { // Enter
if ( !isIE ) { $file.trigger('click'); }
} else if ( e.which === 8 || e.which === 46 ) { // Backspace & Del
// On some browsers the value is read-only
// with this trick we remove the old input and add
// a clean clone with all the original events attached
$file.replaceWith( $file = $file.clone( true ) );
$file.trigger('change');
$input.val('');
} else if ( e.which === 9 ){ // TAB
return;
} else { // All other keys
return false;
}
}
});
});
};
// Old browser fallback
if ( !multipleSupport ) {
$( document ).on('change', 'input.customfile', function() {
var $this = $(this),
// Create a unique ID so we
// can attach the label to the input
uniqId = 'customfile_'+ (new Date()).getTime(),
$wrap = $this.parent(),
// Filter empty input
$inputs = $wrap.siblings().find('.file-upload-input')
.filter(function(){ return !this.value }),
$file = $('<input type="file" id="'+ uniqId +'" name="'+ $this.attr('name') +'"/>');
// 1ms timeout so it runs after all other events
// that modify the value have triggered
setTimeout(function() {
// Add a new input
if ( $this.val() ) {
// Check for empty fields to prevent
// creating new inputs when changing files
if ( !$inputs.length ) {
$wrap.after( $file );
$file.customFile();
}
// Remove and reorganize inputs
} else {
$inputs.parent().remove();
// Move the input so it's always last on the list
$wrap.appendTo( $wrap.parent() );
$wrap.find('input').focus();
}
}, 1);
});
}
}(jQuery));
$('input[type=file]').customFile();