Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-11699 Read files as Data URLs properly #191

Merged
merged 1 commit into from Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 35 additions & 1 deletion tests/tests.js
Expand Up @@ -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) {
Expand Down
14 changes: 12 additions & 2 deletions www/FileReader.js
Expand Up @@ -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}));
Expand All @@ -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);
}
Expand Down