diff options
-rw-r--r-- | binaryajax.js | 57 |
1 files changed, 48 insertions, 9 deletions
diff --git a/binaryajax.js b/binaryajax.js index b721a9c..f7b5b48 100644 --- a/binaryajax.js +++ b/binaryajax.js @@ -1,6 +1,6 @@ /* - * Binary Ajax 0.1.7 + * Binary Ajax 0.1.10 * Copyright (c) 2008 Jacob Seidelin, cupboy@gmail.com, http://blog.nihilogic.dk/ * Licensed under the MPL License [http://www.nihilogic.dk/licenses/mpl-license.txt] */ @@ -21,12 +21,26 @@ var BinaryFile = function(strData, iDataOffset, iDataLength) { this.getByteAt = function(iOffset) { return data.charCodeAt(iOffset + dataOffset) & 0xFF; } + + this.getBytesAt = function(iOffset, iLength) { + var aBytes = []; + + for (var i = 0; i < iLength; i++) { + aBytes[i] = data.charCodeAt((iOffset + i) + dataOffset) & 0xFF + }; + + return aBytes; + } } else if (typeof strData == "unknown") { dataLength = iDataLength || IEBinary_getLength(data); this.getByteAt = function(iOffset) { return IEBinary_getByteAt(data, iOffset + dataOffset); } + + this.getBytesAt = function(iOffset, iLength) { + return new VBArray(IEBinary_getBytesAt(data, iOffset + dataOffset, iLength)).toArray(); + } } this.getLength = function() { @@ -74,10 +88,13 @@ var BinaryFile = function(strData, iDataOffset, iDataLength) { else return iULong; } + this.getStringAt = function(iOffset, iLength) { var aStr = []; - for (var i=iOffset,j=0;i<iOffset+iLength;i++,j++) { - aStr[j] = String.fromCharCode(this.getByteAt(i)); + + var aBytes = this.getBytesAt(iOffset, iLength); + for (var j=0; j < iLength; j++) { + aStr[j] = String.fromCharCode(aBytes[j]); } return aStr.join(""); } @@ -98,10 +115,10 @@ var BinaryAjax = (function() { function createRequest() { var oHTTP = null; - if (window.XMLHttpRequest) { - oHTTP = new XMLHttpRequest(); - } else if (window.ActiveXObject) { + if (window.ActiveXObject) { oHTTP = new ActiveXObject("Microsoft.XMLHTTP"); + } else if (window.XMLHttpRequest) { + oHTTP = new XMLHttpRequest(); } return oHTTP; } @@ -155,7 +172,6 @@ var BinaryAjax = (function() { if (fncCallback) { if (typeof(oHTTP.onload) != "undefined") { oHTTP.onload = function() { - if (oHTTP.status == "200" || oHTTP.status == "206" || oHTTP.status == "0") { oHTTP.binaryResponse = new BinaryFile(oHTTP.responseText, iDataOffset, iDataLen); oHTTP.fileSize = iFileSize || oHTTP.getResponseHeader("Content-Length"); @@ -172,7 +188,10 @@ var BinaryAjax = (function() { // IE6 craps if we try to extend the XHR object var oRes = { status : oHTTP.status, - binaryResponse : new BinaryFile(oHTTP.responseBody, iDataOffset, iDataLen), + // IE needs responseBody, Chrome/Safari needs responseText + binaryResponse : new BinaryFile( + typeof oHTTP.responseBody == "unknown" ? oHTTP.responseBody : oHTTP.responseText, iDataOffset, iDataLen + ), fileSize : iFileSize || oHTTP.getResponseHeader("Content-Length") }; fncCallback(oRes); @@ -226,7 +245,7 @@ var BinaryAjax = (function() { }()); - +/* document.write( "<script type='text/vbscript'>\r\n" + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n" @@ -237,3 +256,23 @@ document.write( + "End Function\r\n" + "</script>\r\n" ); +*/ + +document.write( + "<script type='text/vbscript'>\r\n" + + "Function IEBinary_getByteAt(strBinary, iOffset)\r\n" + + " IEBinary_getByteAt = AscB(MidB(strBinary, iOffset + 1, 1))\r\n" + + "End Function\r\n" + + "Function IEBinary_getBytesAt(strBinary, iOffset, iLength)\r\n" + + " Dim aBytes()\r\n" + + " ReDim aBytes(iLength - 1)\r\n" + + " For i = 0 To iLength - 1\r\n" + + " aBytes(i) = IEBinary_getByteAt(strBinary, iOffset + i)\r\n" + + " Next\r\n" + + " IEBinary_getBytesAt = aBytes\r\n" + + "End Function\r\n" + + "Function IEBinary_getLength(strBinary)\r\n" + + " IEBinary_getLength = LenB(strBinary)\r\n" + + "End Function\r\n" + + "</script>\r\n" +); |