Skip to content

Commit a796cd7

Browse files
author
Vladimir Demidov
committed
Merge branch 'dev' of https://github.com/mailru/FileAPI into dev
Conflicts: lib/FileAPI.core.js
2 parents cd65c79 + 46822e8 commit a796cd7

File tree

6 files changed

+137
-114
lines changed

6 files changed

+137
-114
lines changed

FileAPI.min.js

Lines changed: 66 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ All the other codes - fatal error, user's involvement is recommend.
529529
header('Access-Control-Allow-Methods: POST, OPTIONS');
530530
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type'); // and other custom headers
531531
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); // a comma-separated list of domains
532+
header('Access-Control-Allow-Credentials: true');
532533

533534
if( $_SERVER['REQUEST_METHOD'] == 'OPTIONS' ){
534535
exit;
@@ -670,11 +671,17 @@ All the other codes - fatal error, user's involvement is recommend.
670671
671672
672673
## Changelog
674+
* [#91](https://github.com/mailru/FileAPI/issues/91): replace `new Image` to `FileAPI.newImage`
675+
* + FileAPI.withCredentials: true
676+
* [#90](https://github.com/mailru/FileAPI/issues/90): Fixed `progress` event
677+
678+
673679
### 1.2.5
674680
* [#86](https://github.com/mailru/FileAPI/issues/86): Smarter upload recovery
675681
* [#87](https://github.com/mailru/FileAPI/issues/87): Fixed upload files into browsers that do not support FormData
676682
* Fixed support "accept" attribute for Flash.
677683
* Fixed detection of HTML5 support for FireFox 3.6
684+
* + FileAPI.html5 option, default "true"
678685
679686
680687
### 1.2.4

index.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,12 +335,14 @@ function onFiles(files){
335335
}
336336

337337
FileAPI.each(result.images, function (dataURL, name){
338-
$('<div/>')
339-
.append('<div><b>'+name+'</b></div>')
340-
.append($(new Image).attr('src', dataURL))
341-
.css({ margin: 5, border: '1px dotted #666', padding: 5 })
342-
.appendTo('body')
343-
;
338+
FileAPI.newImage(dataURL, function (err, img){
339+
$('<div/>')
340+
.append('<div><b>'+name+'</b></div>')
341+
.append(img)
342+
.css({ margin: 5, border: '1px dotted #666', padding: 5 })
343+
.appendTo('body')
344+
;
345+
});
344346
});
345347

346348
document.getElementById('Log').innerHTML += '<pre style="font-size: 11px;">'+xhr.responseText.substr(0, 200)+'</pre>';

lib/FileAPI.Flash.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
})();
2424

2525

26-
if( 1 && api.support.flash && (0 || !api.support.html5 || api.cors && !api.support.cors) ) (function (){
26+
if( 1 && api.support.flash && (0 || !api.html5 || !api.support.html5 || api.cors && !api.support.cors) ) (function (){
2727
var
2828
_attr = api.uid()
2929
, _retry = 0
@@ -366,12 +366,7 @@
366366
}, base64, fn);
367367
}
368368
else {
369-
var img = new Image;
370-
api.event.one(img, 'error abort load', function (evt){
371-
fn(evt.type != 'load' && evt.type, img);
372-
img = null;
373-
});
374-
img.src = 'data:'+ file.type +';base64,'+ base64;
369+
api.newImage('data:'+ file.type +';base64,'+ base64, fn);
375370
}
376371
})
377372
});
@@ -644,12 +639,9 @@
644639

645640

646641
// Check dataURI support
647-
var dataURICheck = new Image;
648-
api.event.one(dataURICheck, 'error load', function (){
649-
api.support.dataURI = !(dataURICheck.width != 1 || dataURICheck.height != 1);
650-
dataURICheck = null;
642+
api.newImage('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==', function (err, img){
643+
api.support.dataURI = !(img.width != 1 || img.height != 1);
651644
flash.init();
652645
});
653-
dataURICheck.src = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
654646
})();
655647
})(FileAPI, window, document);

lib/FileAPI.XHR.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@
140140
}
141141

142142
xhr.open('POST', url, true);
143-
xhr.withCredential = "true";
143+
144+
if( api.withCredentials ){
145+
xhr.withCredentials = "true";
146+
}
144147

145148
if( !options.headers || !options.headers['X-Requested-With'] ){
146149
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

lib/FileAPI.core.js

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
jQuery = window.jQuery,
2020

2121
html5 = !!(File && (FileReader && (window.Uint8Array || FormData || XMLHttpRequest.prototype.sendAsBinary)))
22-
&& !(/safari\//.test(userAgent) && /windows/i.test(userAgent)), // BugFix: https://github.com/mailru/FileAPI/issues/25
22+
&& !(/safari\//i.test(userAgent) && !/chrome\//i.test(userAgent) && /windows/i.test(userAgent)), // BugFix: https://github.com/mailru/FileAPI/issues/25
2323

2424
cors = html5 && ('withCredentials' in (new XMLHttpRequest)),
2525

@@ -59,9 +59,10 @@
5959
version: '1.2.5',
6060

6161
cors: false,
62+
html5: true,
6263
debug: false,
6364
pingUrl: false,
64-
flashAbortTimeout: 0,
65+
withCredentials: true,
6566

6667
staticPath: './',
6768

@@ -104,6 +105,29 @@
104105
}
105106
},
106107

108+
/**
109+
* Create new image
110+
*
111+
* @param {String} [src]
112+
* @param {Function} [fn] 1. error -- boolean, 2. img -- Image element
113+
* @returns {HTMLElement}
114+
*/
115+
newImage: function (src, fn){
116+
var img = document.createElement('img');
117+
if( fn ){
118+
api.event.one(img, 'error load', function (evt){
119+
fn(evt.type == 'error', img);
120+
img = null;
121+
});
122+
}
123+
img.src = src;
124+
return img;
125+
},
126+
127+
/**
128+
* Get XHR
129+
* @returns {XMLHttpRequest}
130+
*/
107131
getXHR: function (){
108132
var xhr;
109133

@@ -477,8 +501,7 @@
477501
}
478502
else {
479503
// Created image
480-
var img = new Image;
481-
img.src = file.dataURL || file;
504+
var img = api.newImage(file.dataURL || file);
482505
api.readAsImage(img, fn, progress);
483506
}
484507
},
@@ -740,10 +763,8 @@
740763
var
741764
proxyXHR = new api.XHR(options)
742765
, dataArray = this._getFilesDataArray(options.files)
743-
, total = 0
744-
, loaded = 0
766+
, _total = 0
745767
, _loaded = 0
746-
, _part = 1 // part of total size
747768
, _this = this
748769
, _nextFile
749770
, _complete = false
@@ -752,7 +773,7 @@
752773

753774
// calc total size
754775
_each(dataArray, function (data){
755-
total += data.size;
776+
_total += data.size;
756777
});
757778

758779
// Array of files
@@ -762,7 +783,7 @@
762783
});
763784

764785
// Set upload status props
765-
proxyXHR.total = total;
786+
proxyXHR.total = _total;
766787
proxyXHR.loaded = 0;
767788

768789
// emit "beforeupload" event
@@ -792,7 +813,7 @@
792813
_file && options.prepare(_file, _fileOptions);
793814

794815
this._getFormData(_fileOptions, data, function (form){
795-
if( !loaded ){
816+
if( !_loaded ){
796817
// emit "upload" event
797818
options.upload(proxyXHR, options);
798819
}
@@ -806,38 +827,39 @@
806827

807828
progress: _file ? function (evt){
808829
if( !_fileLoaded ){
809-
loaded += (total * _part * (evt.loaded/evt.total) - _loaded + .5)|0;
810-
_loaded = loaded;
811-
812-
data.total = evt.total;
813-
data.loaded = evt.loaded;
814-
815830
// emit "fileprogress" event
816-
options.fileprogress(evt, _file, xhr, _fileOptions);
831+
options.fileprogress({
832+
type: 'progress'
833+
, total: data.total = evt.total
834+
, loaded: data.loaded = evt.loaded
835+
}, _file, xhr, _fileOptions);
817836

818837
// emit "progress" event
819838
options.progress({
820-
type: evt.type
821-
, total: total
822-
, loaded: proxyXHR.loaded = loaded
823-
, lengthComputable: true
839+
type: 'progress'
840+
, total: _total
841+
, loaded: proxyXHR.loaded = (_loaded + data.size * (evt.loaded/evt.total))|0
824842
}, _file, xhr, _fileOptions);
825843
}
826844
} : noop,
827845

828846
complete: function (err){
847+
// fixed throttle event
848+
_fileLoaded = true;
849+
829850
_each(_xhrPropsExport, function (name){
830851
proxyXHR[name] = xhr[name];
831852
});
832853

833-
// fixed throttle event
834-
_fileLoaded = true;
835-
836854
if( _file ){
837855
data.loaded = data.total;
838856

857+
// emulate 100% "progress"
858+
this.progress(data);
859+
839860
// bytes loaded
840-
proxyXHR.loaded = (loaded += (loaded - _loaded) + (total*_part + .5)|0);
861+
_loaded += data.size; // data.size != data.total, it's desirable fix this
862+
proxyXHR.loaded = _loaded;
841863

842864
// emit "filecomplete" event
843865
options.filecomplete(err, xhr, _file, _fileOptions);
@@ -849,9 +871,6 @@
849871
})); // xhr
850872

851873

852-
// share of file size from the total size
853-
_part = data.size / total;
854-
855874
// ...
856875
proxyXHR.abort = function (current){ this.current = current; xhr.abort(); };
857876

@@ -872,7 +891,7 @@
872891
files = api._getFilesDataArray([].concat(files));
873892

874893
_each(files, function (data) {
875-
total += data.size;
894+
_total += data.size;
876895
proxyXHR.files.push(data.file);
877896
if (first) {
878897
dataArray.unshift(data);

0 commit comments

Comments
 (0)