2016-06-01 59 views
-4

我的要求是將文件/圖像從現有文件夾複製到另一個文件夾。我怎樣才能用帆JS 實現它我試着用這樣的:我們如何使用Sails js或Node js複製或移動圖像/文件

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

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

但隨着安慰的錯誤:

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' }

+0

看看節點fs https://nodejs.org/api/fs.html –

+1

不會出現,你甚至沒有嘗試過這個網頁搜索。在這裏問問題之前,你需要做基礎研究 – charlietfl

回答

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


}); 
+0

我之前嘗試過,但給我看錯誤: throw er; //未處理的「錯誤」事件 ^ 錯誤:ENOENT,開放 –

+0

給予正確的文件路徑 –

+0

安慰在終端的路徑是否正確通過終端 –

相關問題