How to copy an image?

I want to copy the image.pngform /folder1in /folder2, how to do it?

/folder1
  image.png
/folder2

Thank!

+3
source share
2 answers

Try something like this:

var fs = require('fs');

var inStr = fs.createReadStream('/your/path/to/file');
var outStr = fs.createWriteStream('/your/path/to/destination');

inStr.pipe(outStr);

The code is not tested, just written from memory.

+10
source

Or, if you prefer callbacks:

fs = require('fs')
fs.readFile('folder1/image.png', function (err, data) {
    if (err) throw err;
    fs.writeFile('folder2/image.png', data, function (err) {
        if (err) throw err;
        console.log('It\ saved!');
    });
});
+8
source

Source: https://habr.com/ru/post/1796663/


All Articles