The example in this article describes how JScript reads and writes binary files. Share to everyone for your reference. The implementation method is as follows:
var bin = new Array (256); for (Var i=0;i<256;i++) {Bin[i]=string.fromcharcode (i);} function Testwrite () {var Stream = new ActiveXObject ("Adod
B.stream ");
var adtypebinary=1,adtypetext=2;
Stream.type = adTypeText;
Stream.charset = "Iso-8859-1";
Stream.open ();
Stream.writetext ("\x00\x01\x02\xff\xff");
for (Var i=0;i<256;i++) {Stream.writetext (String.fromCharCode (i));
Stream.writetext (Bin[i]);
} stream.savetofile ("C:\\windows\\temp\\test.bin", 2);
Stream.Close ();
Stream = null;
function Binaryfile (filepath) {var adtypebinary=1,adtypetext=2;
var adsavecreatenotexist=1,adsavecreateoverwrite=2;
var adreadall=-1,adreadline=-2;
This.path=filepath; This. Writeall = function (content) {var Stream = new ActiveXObject ("ADODB.")
Stream ");
Stream.type = adTypeText;
Stream.charset = "Iso-8859-1";
Stream.open ();
Stream.writetext (content);
Stream.savetofile (This.path, adsavecreateoverwrite);
Stream.Close ();
Stream = null; } this. ReadAll = function () {var Stream = new ActiveXObject ("ADODB.
Stream ");
Stream.type = adTypeText;
Stream.charset = "Iso-8859-1";
Stream.open ();
Stream.loadfromfile (This.path);
var content = Stream.readtext (Adreadall);
Stream.Close ();
Stream = null;
return content; }
}
Examples of usage are as follows:
var crfolder = ' c:/temp/cr '
var bf1=new binaryfile (crfolder+ "/pcdv0026". JPG ");
var bf2=new binaryfile (crfolder+ "/pcdv0026_. JPG ");
Bf2. Writeall (Bf1. ReadAll ());
I hope this article will help you with your JavaScript programming.