0

In my current codes, it does only can read a text file, How can I make an Image (base64) file opened with Photos Application (Windows)? Is there any chance to do that? If it's impossible, please let me know!

const fs = require('fs')

fs.readFile('./Test/a.txt', 'utf8' , (err, data) => {
    if (err) {
      console.error(err)
      return
    }
    console.log(data)
    return
})

2 Answers 2

2

Another possible solution is like this:

const cp = require('child_process');

const imageFilePath = '/aaa/bbb/ccc'

const c = cp.spawn('a_program_that_opens_images', [ `"${imageFilePath}"` ]);  


c.stdout.pipe(process.stdout);  
c.stderr.pipe(process.stderr);


c.once('exit', exitCode => { 

   // child process has exited

});
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't work well on mine, It gives me an output something like this: throw er; // Unhandled 'error' event, errno: -4058, code: 'ENOENT', syscall: 'spawn program_that_opens_images', path: 'program_that_opens_images', spawnargs: [ 'C:/Users/MyUser/Desktop/MYFolder/Test/MyPic.png' ]
Because "program_that_opens_images" is not a real program - put the right path to the program there
Figure it out? Happy to provide more input
0

Do something like this:

const cp = require('child_process');
const c = cp.spawn('bash');  // 1

const imageFilePath = '/aaa/bbb/ccc'

c.stdin.end(` 
   program_that_opens_images "${imageFilePath}"
`);  // 2

c.stdout.pipe(process.stdout);  // 3
c.stderr.pipe(process.stderr);

c.once('exit', exitCode => {    // 4
   // child process has exited

});

what it does:

  1. spawns a bash child process (use sh or zsh instead if you want)
  2. writes to bash stdin, (inputting the command to run)
  3. pipes the stdio from the child to the parent
  4. captures the exit code from the child

1 Comment

note on windows, you may wish to use my other answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.