Read and Write binary files using JScript

Source: Internet
Author: User

In the ASP environment, VBScript is more convenient than JScript in reading and writing binary data. JScript itself does not have data types, objects, and methods for binary data (or byte data, therefore, some experts love to use JScript to write ASP, but if it involves byte data processing, it is basically a headache. You can only read and write in text mode with ADODB. Stream, and you need to set the character set to a single-byte character set, such as a iso-8859-1 ).

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 ");
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 code
/*

VaR crfolder = 'C:/temp/Cr'
VaR bf1 = new binaryfile (crfolder + "/pcdv0026.jpg ");
VaR bf2 = new binaryfile (crfolder + "/pcdv0026_.jpg ");
Bf2.writeall (bf1.readall ());
*/

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.