Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a image?

Tags:

node.js

fs

I want to copy image.png form /folder1 to /folder2, how to do it?

/folder1
  image.png
/folder2

Thanks!

like image 682
Christopher Morin Avatar asked Mar 06 '11 17:03

Christopher Morin


People also ask

How do you copy a picture and paste it?

Select what you want to copy. Tap Copy. Touch & hold where you want to paste. Tap Paste.

Can I copy an image from the Internet?

The vast majority of images on the internet are likely to be protected by copyright. The only way to be 100% sure that an image is safe to copy and use is to seek permission before you do so.

How do I copy a picture from Google?

Once in Google Photos, find the photo/video that you wish to save and select it. Then, tap the three-dot icon and select Save to device or Download from the menu. This will save the photo/video on your Android/iOS phone or tablet.


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

Code is not tested, just written down from memory.

like image 185
schaermu Avatar answered Oct 20 '22 13:10

schaermu


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\'s saved!');
    });
});
like image 41
Jonathan Avatar answered Oct 20 '22 13:10

Jonathan