我们如何使用Sails js或Node js复制或移动图像/文件

时间:2016-06-01 11:57:31

标签: angularjs node.js api sails.js

我的要求是将文件/图像从现有文件夹复制到另一个文件夹。我怎样才能使用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' }

1 个答案:

答案 0 :(得分:0)

//使用此代码

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


});