因此,这里是一个很好的方式访问的二进制文件从JScript的。如果您要访问的这个对象从VBScript中,然后把它放在一个SWF文件。此对象已经被用于在愤怒将文件上载到Web服务,并已被证明是好的和快速的文件,在0-10 MB的地区。/** This is a fairly well optimized object which alows 访问的二进制文件从JScript的在Windows *作业系统。 *该文件的末尾是小的一套测试,以显示它如何 *用。您将需要adodb 2.5或更高版本。 *这将是使上最2000年机器和所有XP或更高 *机器。 * *版权:博士亚历山大j特纳-保留所有权利。 *请随时与使用此代码在任何您喜欢的方式 *只要您一个参考的地方,在评论 *我写的。 function BinaryFile(name) { var adTypeBinary = 1 var adTypeText = 2 var adSaveCreateOverWrite = 2 // The trick - this is the 'old fassioned' not translation page // It lest javascript use strings to act like raw octets var codePage='437';
this.path=name;
var forward = new Array(); var backward = new Array();
var hD="0123456789ABCDEF"; this.d2h = function(d) { var h = hD.substr(d&15,1); while(d>15) {d>>=4;h=hD.substr(d&15,1)+h;} return h; }
this.h2d = function(h) { return parseInt(h,16); }
this.WriteAll = function(what) { //Create Stream object var BinaryStream = WScript.CreateObject("ADODB.Stream"); //Specify stream type - we cheat and get string but 'like' binary BinaryStream.Type = adTypeText; BinaryStream.CharSet = '437'; //Open the stream BinaryStream.Open(); // Write to the stream BinaryStream.WriteText(this.Forward437(what)); // Write the string to the disk BinaryStream.SaveToFile(this.path, adSaveCreateOverWrite);
// Clearn up BinaryStream.Close(); }
this.ReadAll = function() { //Create Stream object - needs ADO 2.5 or heigher var BinaryStream = WScript.CreateObject("ADODB.Stream") //Specify stream type - we cheat and get string but 'like' binary BinaryStream.Type = adTypeText; BinaryStream.CharSet = codePage; //Open the stream BinaryStream.Open(); //Load the file data from disk To stream object BinaryStream.LoadFromFile(this.path); //Open the stream And get binary 'string' from the object var what = BinaryStream.ReadText; // Clean up BinaryStream.Close(); return this.Backward437(what); }
/* Convert a octet number to a code page 437 char code */ this.Forward437 = function(inString) { var encArray = new Array(); var tmp=''; var i=0; var c=0; var l=inString.length; var cc; var h; for(;i<l;++i) { c++; if(c==128) { encArray.push(tmp); tmp=''; c=0; } cc=inString.charCodeAt(i); if(cc<128) { tmp+=String.fromCharCode(cc); } else { h=this.d2h(cc); h=forward[''+h]; tmp+=String.fromCharCode(this.h2d(h)); } } if(tmp!='') { encArray.push(tmp); }
// this loop progressive concatonates the // array elements entil there is only one var ar2=new Array(); for(;encArray.length>1;) { var l=encArray.length; for(var c=0;c<l;c+=2) { if(c+1==l) { ar2.push(encArray[c]); } else { ar2.push(''+encArray[c]+encArray[c+1]); } } encArray=ar2; ar2=new Array(); } return encArray[0]; } /* Convert a code page 437 char code to a octet number*/ this.Backward437 = function(inString) { var encArray = new Array(); var tmp=''; var i=0; var c=0; var l=inString.length; var cc; var h; for(;i<l;++i) { c++; if(c==128) { encArray.push(tmp); tmp=''; c=0; } cc=inString.charCodeAt(i); if(cc<128) { tmp+=String.fromCharCode(cc); } else { h=this.d2h(cc); h=backward[''+h]; tmp+=String.fromCharCode(this.h2d(h)); } } if(tmp!='') { encArray.push(tmp); }
// this loop progressive concatonates the // array elements entil there is only one var ar2=new Array(); for(;encArray.length>1;) { var l=encArray.length; for(var c=0;c<l;c+=2) { if(c+1==l) { ar2.push(encArray[c]); } else { ar2.push(''+encArray[c]+encArray[c+1]); } } encArray=ar2; ar2=new Array(); } return encArray[0]; }
}
// 使用方法: /* var bf0=new BinaryFile(); var crFolder = 'C:/Temp/cr' var bf1=new BinaryFile(crFolder+"/PCDV0026.JPG"); var bf2=new BinaryFile(crFolder+"/PCDV0026_.JPG"); bf2.WriteAll(bf1.ReadAll()); */