Home > Web Front-end > JS Tutorial > How to read and write binary files in jscript_javascript skills

How to read and write binary files in jscript_javascript skills

WBOY
Release: 2016-05-16 16:02:49
Original
1384 people have browsed it

本文实例讲述了jscript读写二进制文件的方法。分享给大家供大家参考。具体实现方法如下:

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;
  }
}
Copy after login

用法示例如下:

var crFolder = 'C:/Temp/cr'
var bf1=new BinaryFile(crFolder+"/PCDV0026.JPG");
var bf2=new BinaryFile(crFolder+"/PCDV0026_.JPG");
bf2.WriteAll(bf1.ReadAll());
Copy after login

希望本文所述对大家的javascript程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template