3

I have a file stored in local computer and a printer connected too. So generally if I want to give a print of the file to the connected printer, I would run following command in command prompt:

cd <path where the file is locally stored>
copy <filename.ext> <portname in which the printer is connected>

So for example, if I have a file at path /Users/chandrani.chatterjee/Desktop/TestFolder and a printer connected at port lpt1 I would write following commands in command prompt

cd /Users/chandrani.chatterjee/Desktop/TestFolder
copy testfilename.txt lpt1

So this would print my testfilename.txt

No I want to achieve this on a button click in ReactJs. I searched the web and found out about shellJs but I am not sure on how to fire the commands using it.

I tried

var sh = require('shelljs');
const { stdout, stderr, code } = sh.exec('cd /Users/chandrani.chatterjee/Desktop/TestFolder', { silent: true });

I also tried

var sh = require('shelljs');
var output = sh.exec('java -version', {silent:true}).stdout;

but getting error in both the scenarios

Cannot read property 'stdout' of null

How to do this correctly?

EDIT:

For example in .Net the same can be achieved with below code:

function create_send_file() { 

                                var fso = new ActiveXObject("Scripting.FileSystemObject"); 
                                var s = fso.CreateTextFile("c:\\FilePrn\\label.prn", true); 
                                s.WriteLine("some string"); 
                                s.Close(); 
                                var newpath = fso.CopyFile("c:\\FilePrn\\label.prn", "lpt1"); 

                        }
Molly
  • 1,887
  • 3
  • 17
  • 34

1 Answers1

4

Your Reactapp is running in the browser so doesn't have access to your shell.

To do this you will need to create a web service that React app can call to. Here you can execute the shell command return the result to the React in the browser.

Cameron Downer
  • 1,839
  • 17
  • 26
  • but in .Net same thing is achievable. .Net also runs in browser. So is there any way to achieve the same in react? – Molly Apr 12 '19 at 12:38
  • I'm not well versed in .Net, but I imagine it's running on the backend and returning the resulting HTML to the browser. React provides the code to the browser to run, and so we cannot access the shell. – Cameron Downer Apr 12 '19 at 12:42
  • please see my updated question. from the frontend we are not calling any api – Molly Apr 12 '19 at 12:48
  • That .Net code will be executing on the server for it to work. This this cannot be done in React alone and will need to be executed by code on the server. – Cameron Downer Apr 12 '19 at 12:52
  • You are comparing apples with pears. As @Molly has stated, ReactJS , is frontend JavaScript code that runs on your browser. It has no direct access to your file system. It would be dangerous if it did. You can’t execute a server-side code directly from it. On the other hand the .Net code in your edited question is server-side code. Hence, you are able to interact directly with IO (file system) code. – Sola Oderinde Nov 12 '22 at 05:27