How to read and write binary files using jscript _ javascript skills

Source: Internet
Author: User
This article mainly introduces how jscript reads and writes binary files. It involves the use of ActiveXObject objects in javascript and has some reference value, for more information about how to read and write binary files using jscript, see the following example. Share it with you for your reference. The specific 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("ADODB.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;  }}

Example:

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 design javascript programs.

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.