gpt4 book ai didi

javascript - 转换可读流以将其保存为本地文件

转载 作者:行者123 更新时间:2023-11-30 14:14:21 25 4
gpt4 key购买 nike

我正在使用 ssh2-sftp-client 从远程服务器获取文件。我正在获取可读流中的文件。我想将此可读流转换为所需的文件(sample.png 作为 png 文件,sample.doc 文件作为 doc 文件等)

这是我的代码-

let Client = require('ssh2-sftp-client');
let sftp = new Client();
sftp.connect({
host: sftpCredentials.host,
port: sftpCredentials.port,
username: sftpCredentials.username,
password: sftpCredentials.password
}).then(res => sftp.list('/'))
.then(res => {
const file = await sftp.get('/sample.png');

})

我想把这个文件保存在本地。文件是一个可读流如下 -

ReadStream {_readableState: ReadableState, readable: true, domain: null, _events: Object, _eventsCount: 3, …}
_events:Object {end: , error: , readable: }
_eventsCount:3
_maxListeners:undefined
_readableState:ReadableState {objectMode: false, highWaterMark: 65536, buffer: BufferList, …}
autoClose:true
destroyed:false
domain:null
end:undefined
flags:"r"
handle:Buffer(4) [0, 0, 0, …]
mode:438
path:"/sample.png"
pos:131072
readable:true
readableHighWaterMark:65536
sftp:SFTPStream {_readableState: ReadableState, readable: true, domain: null, …}
start:undefined
__proto__:Readable {open: , _read: , destroy: , …}

最佳答案

因为file是一个可读流,你可以试试

let Client = require('ssh2-sftp-client');
let fsv=vrequire('fs');
let sftp = new Client();
sftp.connect({
host: sftpCredentials.host,
port: sftpCredentials.port,
username: sftpCredentials.username,
password: sftpCredentials.password
}).then(res => sftp.list('/'))
.then(async res => {
const file = await sftp.get('/sample.png');
const ws=fs.createWriteStream('sample.png');
file.pipe(ws);
file.on('end',()=>console.log('done'));
})

关于javascript - 转换可读流以将其保存为本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53845667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com