Home > database >  How can I open an Image file using fs in NodeJS?
How can I open an Image file using fs in NodeJS?

Time:02-01

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
})

CodePudding user response:

Another possible solution is like this:

const cp = require('child_process');

const imageFilePath = '/aaa/bbb/ccc'

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


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

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

});

CodePudding user response:

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
  •  Tags:  
  • Related