How to copy a image?

若如初见. 提交于 2020-06-24 10:14:29

问题


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

/folder1
  image.png
/folder2

Thanks!


回答1:


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.




回答2:


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!');
    });
});


来源:https://stackoverflow.com/questions/5212293/how-to-copy-a-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!