Skip to content

Commit c4bd123

Browse files
committed
#157: + Flash-fallback for readAsDataURL, readAsBinaryString and readAsText
1 parent c57c2f3 commit c4bd123

8 files changed

Lines changed: 142 additions & 40 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,7 @@ Button like link.
14991499
15001500
### 2.1.0
15011501
<ul>
1502+
<li>+ Flash-fallback for `readAsDataURL`, `readAsBinaryString` and `readAsText`</li>
15021503
<li>+ `FileAPI.upload(url, files[, opts])`</li>
15031504
<li>#134: + `FileAPI.getMimeType(file)`</li>
15041505
<li>+ `serial: true` upload option</li>

dist/FileAPI.html5.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@
657657
_readAs(file, fn, 'BinaryString');
658658
} else {
659659
// Hello IE10!
660-
_readAs(file, function (evt){
660+
api.readAsDataURL(file, function (evt){
661661
if( evt.type == 'load' ){
662662
try {
663663
// dataURL -> binaryString
@@ -668,7 +668,7 @@
668668
}
669669
}
670670
fn(evt);
671-
}, 'DataURL');
671+
});
672672
}
673673
},
674674

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.

dist/FileAPI.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@
657657
_readAs(file, fn, 'BinaryString');
658658
} else {
659659
// Hello IE10!
660-
_readAs(file, function (evt){
660+
api.readAsDataURL(file, function (evt){
661661
if( evt.type == 'load' ){
662662
try {
663663
// dataURL -> binaryString
@@ -668,7 +668,7 @@
668668
}
669669
}
670670
fn(evt);
671-
}, 'DataURL');
671+
});
672672
}
673673
},
674674

@@ -3676,6 +3676,50 @@
36763676

36773677
// FileAPI
36783678
_inherit(api, {
3679+
readAsDataURL: function (file, callback){
3680+
if( _isHtmlFile(file) ){
3681+
this.parent.apply(this, arguments);
3682+
}
3683+
else {
3684+
api.log('FlashAPI.readAsBase64');
3685+
3686+
flash.cmd(file, 'readAsBase64', {
3687+
id: file.id,
3688+
callback: _wrap(function _(err, base64){
3689+
_unwrap(_);
3690+
3691+
api.log('FlashAPI.readAsBase64:', err);
3692+
3693+
callback({
3694+
type: err ? 'error' : 'load'
3695+
, error: err
3696+
, result: 'data:'+ file.type +';base64,'+ base64
3697+
});
3698+
})
3699+
});
3700+
}
3701+
},
3702+
3703+
readAsText: function (file, encoding, callback){
3704+
if( callback ){
3705+
api.log('[warn] FlashAPI.readAsText not supported `encoding` param');
3706+
} else {
3707+
callback = encoding;
3708+
}
3709+
3710+
api.readAsDataURL(file, function (evt){
3711+
if( evt.type == 'load' ){
3712+
try {
3713+
evt.result = window.atob(evt.result.split(';base64,')[1]);
3714+
} catch( err ){
3715+
evt.type = 'error';
3716+
evt.error = err.toString();
3717+
}
3718+
}
3719+
callback(evt);
3720+
});
3721+
},
3722+
36793723
getFiles: function (input, filter, callback){
36803724
if( callback ){
36813725
api.filterFiles(api.getFiles(input), filter, callback);

dist/FileAPI.min.js

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

lib/FileAPI.Flash.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,50 @@
345345

346346
// FileAPI
347347
_inherit(api, {
348+
readAsDataURL: function (file, callback){
349+
if( _isHtmlFile(file) ){
350+
this.parent.apply(this, arguments);
351+
}
352+
else {
353+
api.log('FlashAPI.readAsBase64');
354+
355+
flash.cmd(file, 'readAsBase64', {
356+
id: file.id,
357+
callback: _wrap(function _(err, base64){
358+
_unwrap(_);
359+
360+
api.log('FlashAPI.readAsBase64:', err);
361+
362+
callback({
363+
type: err ? 'error' : 'load'
364+
, error: err
365+
, result: 'data:'+ file.type +';base64,'+ base64
366+
});
367+
})
368+
});
369+
}
370+
},
371+
372+
readAsText: function (file, encoding, callback){
373+
if( callback ){
374+
api.log('[warn] FlashAPI.readAsText not supported `encoding` param');
375+
} else {
376+
callback = encoding;
377+
}
378+
379+
api.readAsDataURL(file, function (evt){
380+
if( evt.type == 'load' ){
381+
try {
382+
evt.result = window.atob(evt.result.split(';base64,')[1]);
383+
} catch( err ){
384+
evt.type = 'error';
385+
evt.error = err.toString();
386+
}
387+
}
388+
callback(evt);
389+
});
390+
},
391+
348392
getFiles: function (input, filter, callback){
349393
if( callback ){
350394
api.filterFiles(api.getFiles(input), filter, callback);

lib/FileAPI.core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@
563563
_readAs(file, fn, 'BinaryString');
564564
} else {
565565
// Hello IE10!
566-
_readAs(file, function (evt){
566+
api.readAsDataURL(file, function (evt){
567567
if( evt.type == 'load' ){
568568
try {
569569
// dataURL -> binaryString
@@ -574,7 +574,7 @@
574574
}
575575
}
576576
fn(evt);
577-
}, 'DataURL');
577+
});
578578
}
579579
},
580580

tests/tests.js

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -139,47 +139,60 @@ module('FileAPI');
139139

140140

141141
test('1px.gif', function (){
142-
var file = FileAPI.getFiles(uploadForm['1px.gif'])[0];
142+
var file = FileAPI.getFiles(uploadForm['1px.gif'])[0];
143+
var queue = FileAPI.queue(start);
144+
145+
stop();
143146

144147
// File
145148
checkFile(file, '1px.gif', 'image/gif', 34);
146149

147150
// File info
148-
stop();
151+
queue.inc();
149152
FileAPI.getInfo(file, function (err, info){
150-
start();
153+
queue.next();
154+
151155
ok(!err);
152156
equal(info.width, 1, 'getInfo.width');
153157
equal(info.height, 1, 'getInfo.height');
154158
});
155159

156160

157-
if( FileAPI.html5 ){
158-
// Read as data
159-
stop();
160-
FileAPI.readAsDataURL(file, function (evt){
161-
if( evt.type == 'load' ){
162-
start();
163-
equal(evt.result, 'data:image/gif;base64,'+base64_1px_gif, 'dataURL');
164-
}
165-
});
161+
// Read as data
162+
queue.inc();
163+
FileAPI.readAsDataURL(file, function (evt){
164+
queue.next();
166165

167-
// Read as binaryString
168-
stop();
169-
FileAPI.readAsBinaryString(file, function (evt){
170-
if( evt.type == 'load' ){
171-
start();
172-
equal(evt.result, FileAPI.toBinaryString(base64_1px_gif), 'dataURL');
173-
}
174-
});
166+
if( evt.type == 'load' ){
167+
equal(evt.result, 'data:image/gif;base64,'+base64_1px_gif, 'readAsDataURL');
168+
} else {
169+
ok(false, 'readAsDataURL: '+evt.error)
170+
}
171+
});
175172

173+
// Read as binaryString
174+
queue.inc();
175+
FileAPI.readAsBinaryString(file, function (evt){
176+
queue.next();
177+
178+
if( evt.type == 'load' ){
179+
equal(evt.result, FileAPI.toBinaryString(base64_1px_gif), 'readAsBinaryString');
180+
} else {
181+
ok(false, 'readAsBinaryString: '+evt.error)
182+
}
183+
});
184+
185+
if( FileAPI.html5 ){
176186
// Read as image
177-
stop();
187+
queue.inc();
178188
FileAPI.readAsImage(file, function (evt){
189+
queue.next();
190+
179191
if( evt.type == 'load' ){
180-
start();
181192
equal(evt.result.width, 1, 'readAsImage.width');
182193
equal(evt.result.height, 1, 'readAsImage.height');
194+
} else {
195+
ok(false, 'readAsImage')
183196
}
184197
});
185198
}
@@ -189,17 +202,17 @@ module('FileAPI');
189202
test('hello.txt', function (){
190203
var file = FileAPI.getFiles(uploadForm['hello.txt'])[0];
191204

205+
stop();
192206
checkFile(file, 'hello.txt', 'text/plain', 15);
193207

194-
if( FileAPI.html5 ){
195-
stop();
196-
FileAPI.readAsText(file, function (evt){
197-
if( evt.type == 'load' ){
198-
start();
199-
equal(evt.result, 'Hello FileAPI!\n');
200-
}
201-
});
202-
}
208+
FileAPI.readAsText(file, function (evt){
209+
start();
210+
if( evt.type == 'load' ){
211+
equal(evt.result, 'Hello FileAPI!\n');
212+
} else {
213+
ok(false, 'readAsText: '+evt.error);
214+
}
215+
});
203216
});
204217

205218

0 commit comments

Comments
 (0)