file-upload.html
8.34 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>File Upload Example</title>
<link rel="stylesheet" href="../assets/css/bootstrap.css" />
<link rel="stylesheet" href="../assets/css/font-awesome.css" />
<!-- fonts -->
<link rel="stylesheet" href="../assets/css/ace-fonts.css" />
<link rel="stylesheet" href="../assets/css/ace.css" />
<!--[if lte IE 9]>
<link rel="stylesheet" href="../assets/css/ace-ie.css" />
<![endif]-->
</head>
<body>
<div class="main-container">
<div class="page-content">
<div class="row">
<div class="center" style="width:400px; margin:12px;">
<h3 class="header blue smaller lighter">
Uploading files to server
</h3>
<!-- our form -->
<form id="myform" method="post" action="file-upload.php">
<input type="file" name="avatar[]" multiple />
<div class="hr hr-12 dotted"></div>
<button type="submit" class="btn btn-sm btn-primary">Submit</button>
<button type="reset" class="btn btn-sm">Reset</button>
</form>
</div>
</div>
</div>
</div>
<!-- basic scripts -->
<!--[if !IE]> -->
<script type="text/javascript">
window.jQuery || document.write("<script src='../assets/js/jquery.js'>"+"<"+"/script>");
</script>
<!-- <![endif]-->
<!--[if IE]>
<script type="text/javascript">
window.jQuery || document.write("<script src='../assets/js/jquery1x.js'>"+"<"+"/script>");
</script>
<![endif]-->
<!-- ace scripts -->
<script src="../assets/js/bootstrap.js"></script>
<script src="../assets/js/ace-elements.js"></script>
<script src="../assets/js/ace.js"></script>
<script type="text/javascript">
jQuery(function($) {
var $form = $('#myform');
//you can have multiple files, or a file input with "multiple" attribute
var file_input = $form.find('input[type=file]');
var upload_in_progress = false;
file_input.ace_file_input({
style : 'well',
btn_choose : 'Select or drop files here',
btn_change: null,
droppable: true,
thumbnail: 'large',
maxSize: 110000,//bytes
allowExt: ["jpeg", "jpg", "png", "gif"],
allowMime: ["image/jpg", "image/jpeg", "image/png", "image/gif"],
before_remove: function() {
if(upload_in_progress)
return false;//if we are in the middle of uploading a file, don't allow resetting file input
return true;
},
preview_error: function(filename , code) {
//code = 1 means file load error
//code = 2 image load error (possibly file is not an image)
//code = 3 preview failed
}
})
file_input.on('file.error.ace', function(ev, info) {
if(info.error_count['ext'] || info.error_count['mime']) alert('Invalid file type! Please select an image!');
if(info.error_count['size']) alert('Invalid file size! Maximum 100KB');
//you can reset previous selection on error
//ev.preventDefault();
//file_input.ace_file_input('reset_input');
});
var ie_timeout = null;//a time for old browsers uploading via iframe
$form.on('submit', function(e) {
e.preventDefault();
var files = file_input.data('ace_input_files');
if( !files || files.length == 0 ) return false;//no files selected
var deferred ;
if( "FormData" in window ) {
//for modern browsers that support FormData and uploading files via ajax
//we can do >>> var formData_object = new FormData($form[0]);
//but IE10 has a problem with that and throws an exception
//and also browser adds and uploads all selected files, not the filtered ones.
//and drag&dropped files won't be uploaded as well
//so we change it to the following to upload only our filtered files
//and to bypass IE10's error
//and to include drag&dropped files as well
formData_object = new FormData();//create empty FormData object
//serialize our form (which excludes file inputs)
$.each($form.serializeArray(), function(i, item) {
//add them one by one to our FormData
formData_object.append(item.name, item.value);
});
//and then add files
$form.find('input[type=file]').each(function(){
var field_name = $(this).attr('name');
//for fields with "multiple" file support, field name should be something like `myfile[]`
var files = $(this).data('ace_input_files');
if(files && files.length > 0) {
for(var f = 0; f < files.length; f++) {
formData_object.append(field_name, files[f]);
}
}
});
upload_in_progress = true;
file_input.ace_file_input('loading', true);
deferred = $.ajax({
url: $form.attr('action'),
type: $form.attr('method'),
processData: false,//important
contentType: false,//important
dataType: 'json',
data: formData_object
/**
,
xhr: function() {
var req = $.ajaxSettings.xhr();
if (req && req.upload) {
req.upload.addEventListener('progress', function(e) {
if(e.lengthComputable) {
var done = e.loaded || e.position, total = e.total || e.totalSize;
var percent = parseInt((done/total)*100) + '%';
//percentage of uploaded file
}
}, false);
}
return req;
},
beforeSend : function() {
},
success : function() {
}*/
})
}
else {
//for older browsers that don't support FormData and uploading files via ajax
//we use an iframe to upload the form(file) without leaving the page
deferred = new $.Deferred //create a custom deferred object
var temporary_iframe_id = 'temporary-iframe-'+(new Date()).getTime()+'-'+(parseInt(Math.random()*1000));
var temp_iframe =
$('<iframe id="'+temporary_iframe_id+'" name="'+temporary_iframe_id+'" \
frameborder="0" width="0" height="0" src="about:blank"\
style="position:absolute; z-index:-1; visibility: hidden;"></iframe>')
.insertAfter($form)
$form.append('<input type="hidden" name="temporary-iframe-id" value="'+temporary_iframe_id+'" />');
temp_iframe.data('deferrer' , deferred);
//we save the deferred object to the iframe and in our server side response
//we use "temporary-iframe-id" to access iframe and its deferred object
$form.attr({
method: 'POST',
enctype: 'multipart/form-data',
target: temporary_iframe_id //important
});
upload_in_progress = true;
file_input.ace_file_input('loading', true);//display an overlay with loading icon
$form.get(0).submit();
//if we don't receive a response after 30 seconds, let's declare it as failed!
ie_timeout = setTimeout(function(){
ie_timeout = null;
temp_iframe.attr('src', 'about:blank').remove();
deferred.reject({'status':'fail', 'message':'Timeout!'});
} , 30000);
}
////////////////////////////
//deferred callbacks, triggered by both ajax and iframe solution
deferred
.done(function(result) {//success
//format of `result` is optional and sent by server
//in this example, it's an array of multiple results for multiple uploaded files
var message = '';
for(var i = 0; i < result.length; i++) {
if(result[i].status == 'OK') {
message += "File successfully saved. Thumbnail is: " + result[i].url
}
else {
message += "File not saved. " + result.message;
}
message += "\n";
}
alert(message);
})
.fail(function(result) {//failure
alert("There was an error");
})
.always(function() {//called on both success and failure
if(ie_timeout) clearTimeout(ie_timeout)
ie_timeout = null;
upload_in_progress = false;
file_input.ace_file_input('loading', false);
});
deferred.promise();
});
//when "reset" button of form is hit, file field will be reset, but the custom UI won't
//so you should reset the ui on your own
$form.on('reset', function() {
$(this).find('input[type=file]').ace_file_input('reset_input_ui');
});
if(location.protocol == 'file:') alert("For uploading to server, you should access this page using 'http' protocal, i.e. via a webserver.");
});
</script>
</body>
</html>