This repository has been archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
jquery.html5_upload.js
executable file
·256 lines (239 loc) · 11 KB
/
jquery.html5_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
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
(function($) {
$.fn.html5_upload = function(options) {
function get_file_name(file) {
return file.name || file.fileName;
}
function get_file_size(file) {
return file.size || file.fileSize;
}
var available_events = ['onStart', 'onStartOne', 'onProgress', 'onFinishOne', 'onFinish', 'onError'];
var options = $.extend({
onStart: function(event, total) {
return true;
},
onStartOne: function(event, name, number, total) {
return true;
},
onProgress: function(event, progress, name, number, total) {
},
onFinishOne: function(event, response, name, number, total) {
},
onFinish: function(event, total) {
},
onError: function(event, name, error) {
},
onBrowserIncompatible: function() {
alert("Sorry, but your browser is incompatible with uploading files using HTML5 (at least, with current preferences.\n Please install the latest version of Firefox, Safari or Chrome");
},
autostart: true,
autoclear: true,
stopOnFirstError: false,
sendBoundary: false,
fieldName: 'user_file[]',//ignore if sendBoundary is false
extraFields: {}, // extra fields to send with file upload request (HTML5 only)
method: 'post',
STATUSES: {
'STARTED' : 'Started',
'PROGRESS' : 'Progress',
'LOADED' : 'Loaded',
'FINISHED' : 'Finished'
},
headers: {
"Cache-Control":"no-cache",
"X-Requested-With":"XMLHttpRequest",
"X-File-Name": function(file){return encodeURIComponent(get_file_name(file))},
"X-File-Size": function(file){return get_file_size(file)},
"X-CSRF-Token": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": function(file){
if (!options.sendBoundary) return 'multipart/form-data';
return false;
}
},
setName: function(text) {},
setStatus: function(text) {},
setProgress: function(value) {},
genName: function(file, number, total) {
return file + "(" + (number+1) + " of " + total + ")";
},
genStatus: function(progress, finished) {
if (finished) {
return options.STATUSES['FINISHED'];
}
if (progress == 0) {
return options.STATUSES['STARTED'];
}
else if (progress == 1) {
return options.STATUSES['LOADED'];
}
else {
return options.STATUSES['PROGRESS'];
}
},
genProgress: function(loaded, total) {
return loaded / total;
}
}, options);
function upload( files ) {
var total = files.length;
var $this = $(this);
if (!$this.triggerHandler('html5_upload.onStart', [total])) {
return false;
}
this.disabled = true;
var uploaded = 0;
var xhr = this.html5_upload['xhr'];
this.html5_upload['continue_after_abort'] = true;
function upload_file(number) {
if (number == total) {
$this.triggerHandler('html5_upload.onFinish', [total]);
options.setStatus(options.genStatus(1, true));
$this.attr("disabled", false);
if (options.autoclear) {
$this.val("");
}
return;
}
var file = files[number];
if (!$this.triggerHandler('html5_upload.onStartOne', [get_file_name(file), number, total])) {
return upload_file(number+1);
}
options.setStatus(options.genStatus(0));
options.setName(options.genName(get_file_name(file), number, total));
options.setProgress(options.genProgress(0, get_file_size(file)));
xhr.upload['onprogress'] = function(rpe) {
$this.triggerHandler('html5_upload.onProgress', [rpe.loaded / rpe.total, get_file_name(file), number, total]);
options.setStatus(options.genStatus(rpe.loaded / rpe.total));
options.setProgress(options.genProgress(rpe.loaded, rpe.total));
};
xhr.onload = function(load) {
if (xhr.status >= 205 || xhr.status < 200) {
$this.triggerHandler('html5_upload.onError', [get_file_name(file), load]);
if (!options.stopOnFirstError) {
upload_file(number+1);
}
}
else {
$this.triggerHandler('html5_upload.onFinishOne', [xhr.responseText, get_file_name(file), number, total]);
options.setStatus(options.genStatus(1, true));
options.setProgress(options.genProgress(get_file_size(file), get_file_size(file)));
upload_file(number+1);
}
};
xhr.onabort = function() {
if ($this[0].html5_upload['continue_after_abort']) {
upload_file(number+1);
}
else {
$this.attr("disabled", false);
if (options.autoclear) {
$this.val("");
}
}
};
xhr.onerror = function(e) {
$this.triggerHandler('html5_upload.onError', [get_file_name(file), e]);
if (!options.stopOnFirstError) {
upload_file(number+1);
}
};
xhr.open(options.method, typeof(options.url) == "function" ? options.url(number) : options.url, true);
$.each(options.headers,function(key,val){
val = typeof(val) == "function" ? val(file) : encodeURIComponent(val); // resolve value
if (val === false) return true; // if resolved value is boolean false, do not send this header
xhr.setRequestHeader(key, val);
});
if (!options.sendBoundary) {
xhr.send(file);
}
else {
if (window.FormData) {//Many thanks to scottt.tw
var f = new FormData();
f.append(typeof(options.fieldName) == "function" ? options.fieldName() : options.fieldName, file);
var extraFields = typeof(options.extraFields) == "function" ? options.extraFields() : options.extraFields;
$.each(extraFields, function(key, val){
f.append(key, val);
});
xhr.send(f);
}
else if (file.getAsBinary) {//Thanks to jm.schelcher
var boundary = '------multipartformboundary' + (new Date).getTime();
var dashdash = '--';
var crlf = '\r\n';
/* Build RFC2388 string. */
var builder = '';
builder += dashdash;
builder += boundary;
builder += crlf;
builder += 'Content-Disposition: form-data; name="'+(typeof(options.fieldName) == "function" ? options.fieldName() : options.fieldName)+'"';
//thanks to [email protected] for this fix
fileName = unescape(encodeURIComponent(get_file_name(file))); //encode_utf8
builder += '; filename="' + fileName + '"';
builder += crlf;
builder += 'Content-Type: ' + file.type;
builder += crlf;
builder += crlf;
/* Append binary data. */
builder += file.getAsBinary();
builder += crlf;
/* Write boundary. */
builder += dashdash;
builder += boundary;
builder += dashdash;
builder += crlf;
xhr.setRequestHeader('content-type', 'multipart/form-data; boundary=' + boundary);
xhr.sendAsBinary(builder);
}
else {
options.onBrowserIncompatible();
}
}
}
upload_file(0);
return true;
}
try {
return this.each(function() {
var file_input = this;
this.html5_upload = {
xhr: new XMLHttpRequest(),
continue_after_abort: true
};
if (options.autostart) {
$(this).bind('change', function(e){
upload.call( e.target, this.files );
});
}
var self = this;
$.each(available_events, function(event) {
if (options[available_events[event]]) {
$(self).bind("html5_upload."+available_events[event], options[available_events[event]]);
}
});
$(this)
.bind('html5_upload.startFromDrop', function( e, dropEvent ){
if ( dropEvent.dataTransfer && dropEvent.dataTransfer.files.length ){
upload.call( file_input, dropEvent.dataTransfer.files );
}
})
.bind('html5_upload.start', upload)
.bind('html5_upload.cancelOne', function() {
this.html5_upload['xhr'].abort();
})
.bind('html5_upload.cancelAll', function() {
this.html5_upload['continue_after_abort'] = false;
this.html5_upload['xhr'].abort();
})
.bind('html5_upload.destroy', function() {
this.html5_upload['continue_after_abort'] = false;
this.xhr.abort();
delete this.html5_upload;
$(this).unbind('html5_upload.*').unbind('change', upload);
});
});
}
catch (ex) {
options.onBrowserIncompatible();
return false;
}
};
})(jQuery);