【问题标题】:How can we copy or move a image/file using Sails js or Node js我们如何使用 Sails js 或 Node js 复制或移动图像/文件
【发布时间】:2016-09-30 18:45:52
【问题描述】:

我的要求是将文件/图像从现有文件夹复制到另一个文件夹。我如何使用 SAILS JS 实现它 我试过这个:

    var imagePath = photoName;  //image source path 
    var proImagePath = './assets/images/profilePics/'; //image destination

    var stream = fs.createReadStream(imagePath);
    stream.on('error', function (error) 
	{
		console.log("Caught", error);
	});
    stream.on('readable', function ()
	{
		stream.read();
		var desti = fs.createWriteStream(proImagePath);		//desti
		stream.pipe(desti);
		stream.on('end',function() {
        stream.close();
		 return res.json(200, {status: 1, message: 'Success.'});
	});

但是用 :

来安慰一个错误

  Caught { [Error: ENOENT, open 'http://192.168.1.64:9002/images/userimages/sijobhai/91121438-7219-4c0f-b27c-516b289614a2.jpg']
  errno: 34,
  code: 'ENOENT',
  path: 'http://192.168.1.64:9002/images/userimages/sijobhai/91121438-7219-4c0f-b27c-516b289614a2.jpg' }

【问题讨论】:

  • 看看node fs nodejs.org/api/fs.html
  • 似乎您甚至没有尝试对此进行网络搜索。在这里提问之前,您应该先进行基础研究

标签: angularjs node.js api sails.js


【解决方案1】:

//使用此代码

var fs = require('fs');

var source = fs.createReadStream('source_file_path');
var desti = fs.createWriteStream('destination_file_path');

source.pipe(desti);
source.on('end',function() {

   source.close();
   //if you want to delete file fron origin.
   fs.unlink(source_file_path);


});

【讨论】:

  • 我之前尝试过但显示错误: throw er; // 未处理的“错误”事件 ^ 错误:ENOENT,打开
  • 终端中的路径控制台在浏览器中正确打开
  • 你在用ubontu吗
  • 是的..使用 ubuntu 并通过终端运行我的应用程序
  • 以root身份运行sails服务器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
相关资源
最近更新 更多