Skip to content
Permalink
Browse files
CB-11699 Read files as Data URLs properly
When reading file as Data URL, CHUNK_SIZE must be divisible by 3,
otherwise the resultant string made by concatenating chunks will
not be a valid Base64 encoded Data URL.

Also Windows do not support reading sliced files as data URLs, so we
set chunk size equal to file size
  • Loading branch information
vladimir-kotikov committed Aug 16, 2016
1 parent 21298dc commit 3bc49455963e873af457a440ea240d5422d01865
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
@@ -2469,7 +2469,41 @@ exports.defineAutoTests = function () {
},
0, -1, largeText);
});
});
it("file.spec.94.6 should read large file in multiple chunks, readAsDataURL", function (done) {
var largeText = "";
for (var i = 0; i < 10; i++) {
largeText += "Test " + i + "\n";
}

// Set the chunk size so that the read will take 5 chunks
FileReader.READ_CHUNK_SIZE = Math.floor(largeText.length / 4) + 1;

var lastProgressValue = 0;
var progressFunc = function (evt) {
expect(evt.total).toBeDefined();
expect(evt.total).toEqual(largeText.length);

expect(evt.loaded).toBeDefined();
expect(evt.loaded).toBeGreaterThan(lastProgressValue);
expect(evt.loaded).toBeLessThan(evt.total + 1);

lastProgressValue = evt.loaded;
};

runReaderTest('readAsDataURL', false, done, progressFunc,
function (evt, fileData, fileDataAsBinaryString) {
expect(function () {
// Cut off data uri prefix
var base64Data = evt.target.result.substring(evt.target.result.indexOf(',') + 1);
expect(window.atob(base64Data)).toEqual(fileData);
}).not.toThrow();

expect(lastProgressValue).toEqual(largeText.length);
done();
},
undefined, undefined, largeText);
});
});
//Read method
describe('FileWriter', function () {
it("file.spec.95 should have correct methods", function (done) {
@@ -121,9 +121,19 @@ function readSuccessCallback(readType, encoding, offset, totalSize, accumulate,
return;
}

var CHUNK_SIZE = FileReader.READ_CHUNK_SIZE;
if (readType === 'readAsDataURL') {
// Windows proxy does not support reading file slices as Data URLs
// so read the whole file at once.
CHUNK_SIZE = cordova.platformId === 'windows' ? totalSize :
// Calculate new chunk size for data URLs to be multiply of 3
// Otherwise concatenated base64 chunks won't be valid base64 data
FileReader.READ_CHUNK_SIZE - (FileReader.READ_CHUNK_SIZE % 3) + 3;
}

if (typeof r !== "undefined") {
accumulate(r);
this._progress = Math.min(this._progress + FileReader.READ_CHUNK_SIZE, totalSize);
this._progress = Math.min(this._progress + CHUNK_SIZE, totalSize);

if (typeof this.onprogress === "function") {
this.onprogress(new ProgressEvent("progress", {loaded:this._progress, total:totalSize}));
@@ -134,7 +144,7 @@ function readSuccessCallback(readType, encoding, offset, totalSize, accumulate,
var execArgs = [
this._localURL,
offset + this._progress,
offset + this._progress + Math.min(totalSize - this._progress, FileReader.READ_CHUNK_SIZE)];
offset + this._progress + Math.min(totalSize - this._progress, CHUNK_SIZE)];
if (encoding) {
execArgs.splice(1, 0, encoding);
}

0 comments on commit 3bc4945

Please sign in to comment.