Using readAsArrayBuffer

This commit is contained in:
Yury Delendik 2012-08-06 12:50:47 -05:00
parent c515bd2c52
commit b272cb7d1d
2 changed files with 38 additions and 9 deletions

View File

@ -124,6 +124,42 @@
};
})();
// No readAsArrayBuffer ?
(function checkFileReaderReadAsArrayBuffer() {
if (typeof FileReader === 'undefined')
return; // FileReader is not implemented
var frPrototype = FileReader.prototype;
// Older versions of Firefox might not have readAsArrayBuffer
if ('readAsArrayBuffer' in frPrototype)
return; // readAsArrayBuffer is implemented
Object.defineProperty(frPrototype, 'readAsArrayBuffer', {
value: function fileReaderReadAsArrayBuffer(blob) {
var fileReader = new FileReader();
var originalReader = this;
fileReader.onload = function fileReaderOnload(evt) {
var data = evt.target.result;
var buffer = new ArrayBuffer(data.length);
var uint8Array = new Uint8Array(buffer);
for (var i = 0; i < data.length; i++)
uint8Array[i] = data.charCodeAt(i);
Object.defineProperty(originalReader, 'result', {
value: buffer,
enumerable: true,
writable: false,
configurable: true
});
var event = document.createEvent('HTMLEvents');
event.initEvent('load', false, false);
originalReader.dispatchEvent(event);
};
fileReader.readAsBinaryString(blob);
}
});
})();
// No XMLHttpRequest.response ?
(function checkXMLHttpRequestResponseCompatibility() {
var xhrPrototype = XMLHttpRequest.prototype;

View File

@ -1944,20 +1944,13 @@ window.addEventListener('change', function webViewerChange(evt) {
// Read the local file into a Uint8Array.
var fileReader = new FileReader();
fileReader.onload = function webViewerChangeFileReaderOnload(evt) {
var data = evt.target.result;
var buffer = new ArrayBuffer(data.length);
var buffer = evt.target.result;
var uint8Array = new Uint8Array(buffer);
for (var i = 0; i < data.length; i++)
uint8Array[i] = data.charCodeAt(i);
PDFView.open(uint8Array, 0);
};
// Read as a binary string since "readAsArrayBuffer" is not yet
// implemented in Firefox.
var file = files[0];
fileReader.readAsBinaryString(file);
fileReader.readAsArrayBuffer(file);
document.title = file.name;
// URL does not reflect proper document location - hiding some icons.