Download from ByteArray / MemoryStream using SSH.NET - File Created at 0K

When I first upload the file and upload it through SSH.NET, everything works fine.

client.DownloadFile(url, x) Using fs= System.IO.File.OpenRead(x) sFtpClient.UploadFile(fs, fn, True) End Using 

However, now I have to (not download the file), but download the file stream:

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

What happens is that the UploadFile method considers that it succeeded, but on the FTP server itself, the file is created with a size of 0 KB.

What am I doing wrong? I also tried adding the buffer size, but it did not work.

I found the code on the Internet. Should I do something like this:

 client.ChangeDirectory(pFileFolder); client.Create(pFileName); client.AppendAllText(pFileName, pContents); 
+7
source share
1 answer

After writing to the stream, the stream pointer is at the end of the stream. Therefore, when you pass a stream to .UploadFile , it reads the stream from the pointer (which is at the end) to the end. Therefore, nothing is written. And no errors are made, because everything behaves as it was designed.

You need to reset the pointer to the beginning before passing the stream to .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) 
+6
source

Source: https://habr.com/ru/post/1244648/


All Articles