Executing bat file in javascript

I

ian.irlandez

Hey all,

I'm trying to execute a bat file on the server in javascript. The
javascript sits on the server as well. I'm currently using:
document.location.href='testme.bat';
However the only thing it does is just opens up the bat file and shows
my bat code in the browser. How can I execute the batch file instead
of opening it and viewing it in the browser?
 
D

David Dorward

I'm trying to execute a bat file on the server in javascript. The
javascript sits on the server as well. I'm currently using:
document.location.href='testme.bat';

So you ask the browser to visit that URL; you could do that with a regular
link you know.
However the only thing it does is just opens up the bat file and shows
my bat code in the browser. How can I execute the batch file instead
of opening it and viewing it in the browser?

You have to configure your webserver to execute it. How you do that depends
on which webserver software you use.
 
D

David Dorward

David said:
You have to configure your webserver to execute it. How you do that
depends on which webserver software you use.

Oh, wait a moment. Execute where? On the server? Or on the computer running
the browser?

If the latter then you need to configure the server to serve the file with a
suitable content type, then tell your browser that that content type should
be executed. That might prove tricky. Web browsers aren't designed to make
it easy to execute downloaded programmes - too many trojans out there.
 
I

ian.irlandez

I'd need to execute it on the server. Currently I have a jsp file, and
I want a bat file to run when a button is pressed. I've seen people
say that I can use scriptlets and use java to invoke the exec() method,
but I've had no luck getting that to work. I later saw a suggestion of
using document.location.href='xxx.bat';. I tried that and I've had
some luck, such that the browser will at least display the code in the
browser, but I'd want the bat file to execute on the server, and not on
the client.
 
A

Andy Fish

well, if you really _must_ write web server code in a bat file, you'll need
to configure the web server such that the bat file extension is treated as a
CGI program rather than as some text to be served to the browser.

However, if you've got a JSP file you clearly are running in a java
environment. why not make a JSP which runs the bat file (or better still,
one that does the processing that's currently coded as batch script). then
just point the button at the URL for the JSP
 
T

Thomas 'PointedEars' Lahn

I'm trying to execute a bat file on the server in javascript. The
javascript sits on the server as well.

Actually, if you trying to use
[...]
document.location.href='testme.bat';

that is most certainly not entirely true. The script snippet above may
be located in a file on the server, but that file is transmitted as a
resource via HTTP to the client and executed there.
However the only thing it does is just opens up the bat file and shows
my bat code in the browser.

Which is exactly what it is supposed to do, unless it is served with a
filename suffix or (better) a MIME type to which the user agent has an
application linked with; in that case the user agent may also issue a
system call to have the file executed client-side after it has been
downloaded.
How can I execute the batch file instead of opening it and viewing it
in the browser?

Because of the above, the script cannot run executables on the server
directly this way; you will need a server-side script, i.e. a script that
is parsed and executed on the server, usually on HTTP request from the
client (which could be performed with the above code but a visible
hyperlink would suffice).

If we are talking about server-side JS, this would be either Server-side
JavaScript with a Netscape Enterprise Server compliant component or
server-side Microsoft JScript within ASP. But ISTM that e.g. PHP's
program execution functions are more suited to the task:

<http://php.net/exec>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top