Upload from ByteArray/MemoryStream using SSH.NET - File gets created empty (with size 0KB)

后端 未结 1 747
礼貌的吻别
礼貌的吻别 2020-12-06 12:11

When I first download a file and upload it via SSH.NET, all works fine.

client.DownloadFile(url, x)
Using fs= System.IO.File.OpenRead(x)
    sFtpClient.Uploa         


        
相关标签:
1条回答
  • 2020-12-06 13:07

    After writing to the stream, the stream pointer is at the end of the stream. So when you pass the stream to the .UploadFile, it reads the stream from the pointer (which is at the end) to the end. Hence, nothing is written. And no error is issued, because everything behaves as designed.

    You need to reset the pointer to the beginning, before passing the stream to the .UploadFile:

    Dim ba As Byte() = client.DownloadData(url)
    Dim stream As New MemoryStream()
    stream.Write(ba, 0, ba.Length)
    ' Reset the pointer
    stream.Position = 0
    sFtpClient.UploadFile(stream, fn, True)
    

    An alternative is to use SSH.NET PipeStream, which has separate read and write pointers.

    0 讨论(0)
提交回复
热议问题