Skip to content

Commit 067683c

Browse files
committed
+ 'dev' branch, v1.2.5
2 parents a6cca92 + 1e0e1e8 commit 067683c

File tree

10 files changed

+284
-115
lines changed

10 files changed

+284
-115
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,11 +1111,20 @@ Button like link.
11111111
</ul>
11121112

11131113

1114+
### 1.2.5
1115+
* [#86](https://github.com/mailru/FileAPI/issues/86): Smarter upload recovery
1116+
* [#87](https://github.com/mailru/FileAPI/issues/87): Fixed upload files into browsers that do not support FormData
1117+
* Fixed support "accept" attribute for Flash.
1118+
* Fixed detection of HTML5 support for FireFox 3.6
1119+
* + FileAPI.html5 option, default "true"
1120+
1121+
11141122
### 1.2.4
1115-
<ul>
1116-
<li>Fixed auto orientation image by EXIF (Flash)</li>
1117-
<li>Fixed image dimensions after rotate (Flash)</li>
1118-
</ul>
1123+
* Fixed auto orientation image by EXIF (Flash)
1124+
* Fixed image dimensions after rotate (Flash)
1125+
* [#82](https://github.com/mailru/FileAPI/issues/82): "undefined" data-fields cause exceptions
1126+
* [#83](https://github.com/mailru/FileAPI/issues/83): Allow requests without files
1127+
* [#84](https://github.com/mailru/FileAPI/pull/84): Fixed connection abort when waiting for connection recovery
11191128

11201129

11211130
### 1.2.3

crossdomain.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<allow-access-from domain="*.rubaxa.org" secure="false"/>
1010
<allow-access-from domain="github.com" secure="false"/>
1111
<allow-access-from domain="*.github.com" secure="false"/>
12+
<allow-access-from domain="github.io" secure="false"/>
13+
<allow-access-from domain="*.github.io" secure="false"/>
1214
<allow-access-from domain="mail.ru" secure="false"/>
1315
<allow-access-from domain="*.mail.ru" secure="false"/>
1416
<allow-access-from domain="imgsmail.ru" secure="false"/>

dist/FileAPI.html5.js

Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@
9595
}(window));
9696

9797
/*jslint evil: true */
98-
/*global window, Image, Blob, XMLHttpRequest, URL, webkitURL, ActiveXObject */
98+
/*global window, Image, URL, webkitURL, ActiveXObject */
9999

100-
(function (window, jQuery, undef){
100+
(function (window, undef){
101101
'use strict';
102102

103103
var
@@ -110,11 +110,16 @@
110110
// https://github.com/blueimp/JavaScript-Load-Image/blob/master/load-image.js#L48
111111
apiURL = (window.createObjectURL && window) || (window.URL && URL.revokeObjectURL && URL) || (window.webkitURL && webkitURL),
112112

113+
Blob = window.Blob,
113114
File = window.File,
114115
FileReader = window.FileReader,
115116
FormData = window.FormData,
116117

117-
html5 = !!(File && (FileReader && window.Uint8Array || FormData))
118+
119+
XMLHttpRequest = window.XMLHttpRequest,
120+
jQuery = window.jQuery,
121+
122+
html5 = !!(File && (FileReader && (window.Uint8Array || FormData || XMLHttpRequest.prototype.sendAsBinary)))
118123
&& !(/safari\//.test(userAgent) && /windows/i.test(userAgent)), // BugFix: https://github.com/mailru/FileAPI/issues/25
119124

120125
cors = html5 && ('withCredentials' in (new XMLHttpRequest)),
@@ -269,6 +274,7 @@
269274
version: '2.0.0b',
270275

271276
cors: false,
277+
html5: true,
272278
debug: false,
273279
pingUrl: false,
274280
multiFlash: false,
@@ -317,11 +323,15 @@
317323
getXHR: function (){
318324
var xhr;
319325

320-
if( window.XMLHttpRequest ){
326+
if( XMLHttpRequest ){
321327
xhr = new XMLHttpRequest;
322328
}
323329
else if( window.ActiveXObject ){
324-
try { xhr = new ActiveXObject('MSXML2.XMLHttp.3.0'); } catch (e) { }
330+
try {
331+
xhr = new ActiveXObject('MSXML2.XMLHttp.3.0');
332+
} catch (e) {
333+
xhr = new ActiveXObject('Microsoft.XMLHTTP');
334+
}
325335
}
326336

327337
return xhr;
@@ -999,6 +1009,11 @@
9991009
, _fileOptions = _simpleClone(options)
10001010
;
10011011

1012+
if( _file && _file.name === api.expando ){
1013+
_file = null;
1014+
api.log('[warn] FileAPI.upload() — called without files');
1015+
}
1016+
10021017
if( ( proxyXHR.statusText != 'abort' || proxyXHR.current ) && data ){
10031018
// Mark active job
10041019
_complete = false;
@@ -1007,7 +1022,7 @@
10071022
proxyXHR.currentFile = _file;
10081023

10091024
// Prepare file options
1010-
options.prepare(_file, _fileOptions);
1025+
_file && options.prepare(_file, _fileOptions);
10111026

10121027
_this._getFormData(_fileOptions, data, function (form){
10131028
if( !_loaded ){
@@ -1017,12 +1032,12 @@
10171032

10181033
var xhr = new api.XHR(api.extend({}, _fileOptions, {
10191034

1020-
upload: function (){
1035+
upload: _file ? function (){
10211036
// emit "fileupload" event
10221037
options.fileupload(_file, xhr, _fileOptions);
1023-
},
1038+
} : noop,
10241039

1025-
progress: function (evt){
1040+
progress: _file ? function (evt){
10261041
if( !_fileLoaded ){
10271042
// emit "fileprogress" event
10281043
options.fileprogress({
@@ -1038,27 +1053,29 @@
10381053
, loaded: proxyXHR.loaded = (_loaded + data.size * (evt.loaded/evt.total))|0
10391054
}, _file, xhr, _fileOptions);
10401055
}
1041-
},
1056+
} : noop,
10421057

10431058
complete: function (err){
1059+
// fixed throttle event
1060+
_fileLoaded = true;
1061+
10441062
_each(_xhrPropsExport, function (name){
10451063
proxyXHR[name] = xhr[name];
10461064
});
10471065

1048-
data.loaded= data.total;
1066+
if( _file ){
1067+
data.loaded= data.total;
10491068

1050-
// emulate 100% "progress"
1051-
this.progress(data);
1069+
// emulate 100% "progress"
1070+
this.progress(data);
10521071

1053-
// bytes loaded
1054-
_loaded += data.size; // data.size != data.total, it's desirable fix this
1055-
proxyXHR.loaded = _loaded;
1072+
// bytes loaded
1073+
_loaded += data.size; // data.size != data.total, it's desirable fix this
1074+
proxyXHR.loaded = _loaded;
10561075

1057-
// emit "filecomplete" event
1058-
options.filecomplete(err, xhr, _file, _fileOptions);
1059-
1060-
// fixed throttle event
1061-
_fileLoaded = true;
1076+
// emit "filecomplete" event
1077+
options.filecomplete(err, xhr, _file, _fileOptions);
1078+
}
10621079

10631080
// upload next file
10641081
_nextFile.call(_this);
@@ -1086,13 +1103,18 @@
10861103

10871104

10881105
// Append more files to the existing request
1089-
proxyXHR.append = function (files) {
1106+
// first - add them to the queue head/tail
1107+
proxyXHR.append = function (files, first) {
10901108
files = api._getFilesDataArray([].concat(files));
10911109

10921110
_each(files, function (data) {
10931111
_total += data.size;
10941112
proxyXHR.files.push(data.file);
1095-
dataArray.push(data);
1113+
if (first) {
1114+
dataArray.unshift(data);
1115+
} else {
1116+
dataArray.push(data);
1117+
}
10961118
});
10971119

10981120
if( _complete ){
@@ -1150,6 +1172,11 @@
11501172
}
11511173
});
11521174

1175+
if( !files.length ){
1176+
// Create fake `file` object
1177+
files.push({ file: { name: api.expando } });
1178+
}
1179+
11531180
returnfiles;
11541181
},
11551182

@@ -1218,7 +1245,7 @@
12181245
queue.next();
12191246
});
12201247
}
1221-
else {
1248+
else if( filename !== api.expando ){
12221249
Form.append(name, file, filename);
12231250
}
12241251
})(file);
@@ -1630,7 +1657,7 @@
16301657
// @configuration
16311658
if( !api.flashUrl ){ api.flashUrl = api.staticPath + 'FileAPI.flash.swf'; }
16321659
if( !api.flashImageUrl ){ api.flashImageUrl = api.staticPath + 'FileAPI.flash.image.swf'; }
1633-
})(window, window.jQuery);
1660+
})(window, void 0);
16341661

16351662
/*global window, FileAPI, document */
16361663

@@ -2032,9 +2059,9 @@
20322059
append: function (name, blob, file, type){
20332060
this.items.push({
20342061
name: name
2035-
, blob: blob && blob.blob || blob
2036-
, file: file || blob.name
2037-
, type:type || blob.type
2062+
, blob: blob && blob.blob || (blob == void 0 ? '' : blob)
2063+
, file: blob && (file || blob.name)
2064+
, type:blob && (type || blob.type)
20382065
});
20392066
},
20402067

@@ -2054,7 +2081,7 @@
20542081
api.log('FileAPI.Form.toHtmlData');
20552082
this.toHtmlData(fn);
20562083
}
2057-
else if( this.multipart ){
2084+
else if( this.multipart || !FormData ){
20582085
api.log('FileAPI.Form.toMultipartData');
20592086
this.toMultipartData(fn);
20602087
}
@@ -2134,7 +2161,7 @@
21342161
}
21352162

21362163
data.start = -1;
2137-
data.end = -1;
2164+
data.end = data.file.FileAPIReadPosition || -1;
21382165
data.retry = 0;
21392166
});
21402167
},
@@ -2164,6 +2191,7 @@
21642191

21652192
toMultipartData: function (fn){
21662193
this._to([], fn, function (file, data, queue, boundary){
2194+
queue.inc();
21672195
_converFile(file, function (file, blob){
21682196
data.push(
21692197
'--_' + boundary + ('\r\nContent-Disposition: form-data; name="'+ file.name +'"'+ (file.file ? '; filename="'+ encodeURIComponent(file.file) +'"' : '')
@@ -2172,6 +2200,7 @@
21722200
+ '\r\n'+ (file.file ? blob : encodeURIComponent(blob))
21732201
+ '\r\n')
21742202
);
2203+
queue.next();
21752204
});
21762205
}, api.expando);
21772206
}
@@ -2182,6 +2211,16 @@
21822211
var blob = file.blob, filename = file.file;
21832212

21842213
if( filename ){
2214+
if( !blob.toDataURL ){
2215+
// The Blob is not an image.
2216+
api.readAsBinaryString(blob, function (evt){
2217+
if( evt.type == 'load' ){
2218+
fn(file, evt.result);
2219+
}
2220+
});
2221+
return;
2222+
}
2223+
21852224
var
21862225
mime = { 'image/jpeg': '.jpe?g', 'image/png': '.png' }
21872226
, type = mime[file.type] ? file.type : 'image/png'
@@ -2190,6 +2229,7 @@
21902229
;
21912230

21922231
if( !filename.match(new RegExp(ext+'$', 'i')) ){
2232+
// Does not change the current extension, but add a new one.
21932233
filename += ext.replace('?', '');
21942234
}
21952235

@@ -2202,11 +2242,12 @@
22022242
}, type, quality);
22032243
}
22042244
else {
2205-
blob = api.toBinaryString(blob.toDataURL(type, quality));
2245+
fn(file, api.toBinaryString(blob.toDataURL(type, quality)));
22062246
}
22072247
}
2208-
2209-
returnblob;
2248+
else {
2249+
fn(file, blob);
2250+
}
22102251
}
22112252

22122253

@@ -2294,6 +2335,7 @@
22942335
},
22952336

22962337
_send: function (options, data){
2338+
22972339
var _this = this, xhr, uid = _this.uid, url = options.url;
22982340

22992341
api.log('XHR._send:', data);
@@ -2352,6 +2394,10 @@
23522394
}
23532395
else {
23542396
// html5
2397+
if (this.xhr && this.xhr.aborted) {
2398+
api.log("Error: already aborted");
2399+
return;
2400+
}
23552401
xhr = _this.xhr = api.getXHR();
23562402

23572403
if (data.params) {
@@ -2387,6 +2433,8 @@
23872433
}
23882434

23892435
xhr.onreadystatechange = function (){
2436+
var lkb = parseInt(xhr.getResponseHeader('X-Last-Known-Byte'), 10);
2437+
23902438
_this.status = xhr.status;
23912439
_this.statusText = xhr.statusText;
23922440
_this.readyState = xhr.readyState;
@@ -2411,9 +2459,7 @@
24112459
options.pause(data.file, options);
24122460

24132461
// smart restart if server reports about the last known byte
2414-
var lkb = xhr.getResponseHeader('X-Last-Known-Byte');
24152462
api.log("X-Last-Known-Byte: " + lkb);
2416-
24172463
if (lkb) {
24182464
data.end = parseInt(lkb, 10);
24192465
} else {
@@ -2436,6 +2482,14 @@
24362482
_this.end(xhr.status);
24372483
} else {
24382484
// next chunk
2485+
2486+
// shift position if server reports about the last known byte
2487+
api.log("X-Last-Known-Byte: " + lkb);
2488+
if (lkb) {
2489+
data.end = lkb;
2490+
}
2491+
data.file.FileAPIReadPosition = data.end;
2492+
24392493
setTimeout(function () {
24402494
_this._send(options, data);
24412495
}, 0);
@@ -2446,7 +2500,7 @@
24462500
};
24472501

24482502
data.start = data.end + 1;
2449-
data.end = Math.min(data.start + options.chunkSize, data.size ) - 1;
2503+
data.end = Math.max(Math.min(data.start + options.chunkSize, data.size ) - 1, data.start);
24502504

24512505
var slice;
24522506
(slice = 'slice') in data.file || (slice = 'mozSlice') in data.file || (slice = 'webkitSlice') in data.file;

dist/FileAPI.html5.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)