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