Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,21 +454,29 @@ exports.prepareContent = function(name, inputData, isBinary, isOptimizedBinarySt

var isBlob = support.blob && (data instanceof Blob || ["[object File]", "[object Blob]"].indexOf(Object.prototype.toString.call(data)) !== -1);

if (isBlob && typeof FileReader !== "undefined") {
return new external.Promise(function (resolve, reject) {
var reader = new FileReader();

reader.onload = function(e) {
resolve(e.target.result);
};
reader.onerror = function(e) {
reject(e.target.error);
};
reader.readAsArrayBuffer(data);
});
} else {
return data;
if (isBlob) {
if (typeof Blob.prototype.arrayBuffer !== "undefined") {
return data.arrayBuffer();
} else if (typeof FileReader !== "undefined") {
return new external.Promise(function (resolve, reject) {
var reader = new FileReader();

reader.onload = function(e) {
resolve(e.target.result);
};
reader.onerror = function(e) {
reject(e.target.error);
};
reader.readAsArrayBuffer(data);
});
} else {
return external.Promise.reject(
new Error(name + " is a Blob, but we have no way of reading it.")
);
}
}

return data;
});

return promise.then(function(data) {
Expand Down
6 changes: 4 additions & 2 deletions test/asserts/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ QUnit.module("file", function () {
if (JSZip.support.blob) {
QUnit.assert.equal(err, null, testName + "no error");
QUnit.assert.ok(blob instanceof Blob, testName + "the result is a instance of Blob");
QUnit.assert.equal(blob.type, "", testName + "the result has the right mime type");
QUnit.assert.equal(blob.size, opts.rawData.length, testName + "the result has the right length");
if (blob instanceof Blob) {
QUnit.assert.equal(blob.type, "", testName + "the result has the right mime type");
QUnit.assert.equal(blob.size, opts.rawData.length, testName + "the result has the right length");
}
} else {
QUnit.assert.equal(blob, null, testName + "no data");
QUnit.assert.ok(err.message.match("not supported by this platform"), testName + "the error message is useful");
Expand Down