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

前端 未结 4 717
[愿得一人]
[愿得一人] 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:55

    Converted the above code to JavaScript. This appears to work for me. Recommend adding a try catch block to the caller. Also, converted to async. I've used this code to save some 90 files at the same time. Since the delete file (necessary as the overwrite fails) is synchronous, it is better moved to a separate function for multiple files.

    function saveFile(sSourceUrl, sDestFile) {
        var objXMLHTTP = new ActiveXObject("MSXML2.XMLHTTP");
        objXMLHTTP.onreadystatechange=function() {
            if (objXMLHTTP.readyState === 4) {
                var objADOStream = new ActiveXObject("ADODB.Stream");
                objADOStream.open();
                objADOStream.type = 1; // Binary
                objADOStream.write(objXMLHTTP.ResponseBody);
                objADOStream.position = 0;
                objADOStream.saveToFile(sDestFile, 2);
                objADOStream.close();
            }
        };
    
        objXMLHTTP.open("GET", sSourceUrl, false);
        objXMLHTTP.send();
    }
    

提交回复
热议问题