Windows Script Host (jscript): how do i download a binary file?

前端 未结 4 656
[愿得一人]
[愿得一人] 2021-02-10 00:45

i\'m trying to automate a file download with Windows Script Host (JScript). i see ADODB.Stream has an Open method whose documentation makes it seem like it should be possible t

4条回答
  •  面向向阳花
    2021-02-10 00:59

    Here is the download code in JScript. Also added some references to API information.

    var Source = WScript.Arguments.Item(0);
    var Target = WScript.Arguments.Item(1);
    var Object = WScript.CreateObject('MSXML2.XMLHTTP');
    
    Object.Open('GET', Source, false);
    Object.Send();
    
    if (Object.Status == 200)
    {
        // Create the Data Stream
        var Stream = WScript.CreateObject('ADODB.Stream');
    
        // Establish the Stream
        Stream.Open();
        Stream.Type = 1; // adTypeBinary
        Stream.Write(Object.ResponseBody);
        Stream.Position = 0;
    
        // Create an Empty Target File
        var File = WScript.CreateObject('Scripting.FileSystemObject');
        if (File.FileExists(Target))
        {
            File.DeleteFile(Target);
        }
    
        // Write the Data Stream to the File
        Stream.SaveToFile(Target, 2); // adSaveCreateOverWrite
        Stream.Close();
    }
    

    ADODB Stream:

    • http://www.w3schools.com/ado/ado_ref_stream.asp
    • http://msdn.microsoft.com/en-us/library/windows/desktop/ms675032.aspx
    • http://msdn.microsoft.com/en-us/library/windows/desktop/ms680846.aspx

    Scripting.FileSystemObject:

    • http://www.w3schools.com/asp/asp_ref_filesystem.asp

提交回复
热议问题