Methods for JScript to read and write binary files

Source: Internet
Author: User

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.