methods for JScript to read and write binary files
This article mainly introduces the methods of JScript reading and writing binary files, involving the use of ActiveXObject objects in JavaScript, which has some reference value, and the friends who need them can refer to
The example in this article tells you how to read and write a binary file by JScript. Share to everyone for your reference. The implementation methods are 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 ("ADODB. Stream ");
var adtypebinary=1,adtypetext=2;
Stream.type = adTypeText;
Stream.charset = "Iso-8859-1";
Stream.open ();
Stream.writetext ("X00x01x02xffxff");
for (Var i=0;i<256;i++) {
Stream.writetext (String.fromCharCode (i));
Stream.writetext (Bin[i]);
}
Stream.savetofile ("C:windowstemptest.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.