0
我試圖使用SSH在客戶端瀏覽器和遠程服務器('雲')之間發送文件。爲此,我開發了一個Node.js網絡服務器作爲中間人。對於文件上傳,我的節點網絡服務器接收來自用戶的請求並使用ssh2模塊將其傳送到遠程服務器。以下是代碼片段。文件上傳的作品完美:從服務器到客戶端的文件傳輸Node.js
var readStream = fs.createReadStream("filename.txt"); // on client's local computer
var writeStream = sftp.createWriteStream("/path/to/file/filename.txt"); // on the remote server
writeStream.on('close', function() {
console.log("- file transferred to cloud");
sftp.end();
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end("The file has been successfully uploaded to the cloud!");
});
readStream.pipe(writeStream);
所以我嘗試用同樣的想法來傳輸文件的另一種方法:從遠程服務器到客戶端,這裏是我的代碼:
var readStream = sftp.createReadStream("/path/to/file/filename.txt"); // on remote server
var writeStream = fs.createWriteStream("path/filename.txt"); // on the client's local computer server
writeStream.on('close', function() {
console.log("- file transferred to cloud");
sftp.end();
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end("The file has been successfully uploaded to the cloud!");
});
readStream.pipe(writeStream);
的問題是服務器上的文件確實被讀取並且在客戶端創建了一個文件 - 但它沒有數據!我無法弄清楚爲什麼數據不會從服務器傳輸到客戶端,但是如果我將文件從客戶端傳輸到服務器,同樣的方法也可以工作。
任何幫助將不勝感激!
我試過了,我得到'權限被拒絕'...雖然我很確定我已經讀取了服務器上的文件。 – AliKhokhar