I am trying to download text file from server to local directory.

If I execute following curl command it copies myfile.txt from server and saves in same directory as newfile.txt.:

curl -o newfile.txt http://myserverip/myfile.txt

I want to automate this by running the command from javascript while loading the webpage.

For example if I open an html page (which runs a javascript) like http://myserverip/getnewfile.html in the browser the myfile.txt from the server should be copied to newfile.txt in the loacl directory.

Can anyone help me to write javascript to execute the curl command?

Please note that the server in local area network and router is configured to allow connections only from white-listed mac ids of local machines so there is no any authentication required to connect to server.

1 Answers

0
alexbuzzbee On

You can't run local command-line commands from JavaScript, because that would be a massive security vulnerability. The tool you want to use for this is either window.open (to open a tab with the file in order to cause the browser to download it) or the fetch API (to retrieve the file for use in JavaScript).