简体   繁体   中英

Strange behavioural difference between vbscript and jscript using ADODB.stream in an HTA application

VBscript example:

Function ADO_WriteToFile(FileURL,data)
Dim arrBytes
    SET ADObj = CreateObject("ADODB.Stream")
    ADObj.Open
    ADObj.Charset = "iso-8859-1" 
    ADObj.Type = adTypeText
    ADObj.WriteText data
    ADObj.SaveToFile FileURL, adSaveCreateOverwrite
    ADObj.Close
    ADObj.Open
    ADObj.Type = adTypeBinary
    ADObj.LoadFromFile FileURL
    ADObj.Position = 3
    arrBytes = ADObj.Read
    ADObj.Position = 0
    ADObj.SetEOS
    ADObj.Write data
    ADObj.SaveToFile FileURL, adSaveCreateOverwrite
    ADObj.Close
End Function

JScript example:

function writeTo(fileName,str) {
var ado =  new ActiveXObject("ADODB.Stream");
ado.Type = 2;
ado.Open();
ado.Position = 0;
ado.WriteText(str,0);
ado.SaveToFile(fileName,2);
ado.Close();
ado.Open();
ado.Type = 1;

ado.Position = 2;//line 19
var temp = ado.Read();
ado.Position = 0;
ado.SetEOS;
ado.Write(temp);
ado.SaveToFile(fileName,2);
ado.Close();
}

Why does the VBScript example work perfectly except for the fact that it can't accept file paths with space in them?

The JScript example errors out with the message "assignment to the parameter is incorrect." line 19. This doesn't happen if I set Position to 0 however:

ado.Position = 0;

i am using this to write binary files to disk btw

Here are some differences:

  • In the VBScript version, position is set to 3; in the JScript version, it is set to 2
  • In the VBScript version, the character set is defined; in the JScript version, it is undefined
  • In the VBScript version, WriteText and write both reference the argument; in the JScript version, only WriteText references it

References

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM