Javascript 将源文件复制到 Nodejs 中的另一个目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38595524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:38:07  来源:igfitidea点击:

Copy a source file to another destination in Nodejs

javascriptnode.jsnwjsfs-extra

提问by Rao Hammas Hussain

I'm trying to copy an image from a folder to another using fs-extra module .

我正在尝试使用 fs-extra module 将图像从一个文件夹复制到另一个文件夹。

var fse = require('fs-extra');

function copyimage() {
  fse.copy('mainisp.jpg', './test', function (err) {     
    if (err) 
      return console.error(err)
  });
}

This is my directory

这是我的目录

and this is the error I get all the time:

这是我一直得到的错误:

Error {errno: -4058, code: "ENOENT", syscall: "lstat", path: "E:\mainisp.jpg", message: "ENOENT: no such file or directory, lstat 'E:\mainisp.jpg'"}

错误 {errno: -4058, code: "ENOENT", syscall: "lstat", path: "E:\mainisp.jpg", message: "ENOENT: no such file or directory, lstat 'E:\mainisp.jpg' "}

and by changing destination to ./test/I get this error

并通过将目的地更改为./test/我收到此错误

Error {errno: -4058, code: "ENOENT", syscall: "lstat", path: "E:\Development\Node apps\Node softwares\Digital_library\mainisp.jpg", message: "ENOENT: no such file or directory, lstat 'E:\Devel… apps\Node softwares\Digital_library\mainisp.jpg'"}

错误 {errno:-4058,代码:“ENOENT”,系统调用:“lstat”,路径:“E:\Development\Node apps\Node softwares\Digital_library\mainisp.jpg”,消息:“ENOENT:没有这样的文件或目录, lstat 'E:\Devel... apps\Node softwares\Digital_library\mainisp.jpg'"}

Note: I'm not testing this in browser. It's an Nwjs app and the pics of error attached are from Nwjs console.

注意:我没有在浏览器中测试这个。这是一个 Nwjs 应用程序,附加的错误图片来自 Nwjs 控制台。

采纳答案by alejandromav

Try:

尝试:

var fs = require('fs-extra');

fs.copySync(path.resolve(__dirname,'./mainisp.jpg'), './test/mainisp.jpg');

As you can see in the error message, you're trying to read the file from E:\mainisp.jpginstead of the current directory.

正如您在错误消息中看到的,您正在尝试从E:\mainisp.jpg当前目录而不是从当前目录读取文件。

You also need to specify the target path with the file, not only the destination folder.

您还需要指定文件的目标路径,而不仅仅是目标文件夹。

回答by peteb

You can do this using the native fsmodule easily using streams.

您可以使用本机fs模块轻松地使用流来做到这一点。

const fs = require('fs');
const path = require('path');

let filename = 'mainisp.jpg';
let src = path.join(__dirname, filename);
let destDir = path.join(__dirname, 'test');

fs.access(destDir, (err) => {
  if(err)
    fs.mkdirSync(destDir);

  copyFile(src, path.join(destDir, filename));
});


function copyFile(src, dest) {

  let readStream = fs.createReadStream(src);

  readStream.once('error', (err) => {
    console.log(err);
  });

  readStream.once('end', () => {
    console.log('done copying');
  });

  readStream.pipe(fs.createWriteStream(dest));
}

回答by Karan Vyas

Try:

尝试:

const fs = require('fs');
fs.copyFileSync(src, dest);