使用 SSH.NET 从 ByteArray/MemoryStream 上传 - 文件创建为空(大小为 0KB)

Upload from ByteArray/MemoryStream using SSH.NET - File gets created empty (with size 0KB)(使用 SSH.NET 从 ByteArray/MemoryStream 上传 - 文件创建为空(大小为 0KB))
本文介绍了使用 SSH.NET 从 ByteArray/MemoryStream 上传 - 文件创建为空(大小为 0KB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

当我第一次下载文件并通过 SSH.NET 上传时,一切正常.

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.UploadFile(fs, fn, True)
End Using

但是我现在必须(不是下载文件)而是上传文件流:

However I must now (not download the file) but upload a stream of the file:

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

发生的事情是 UploadFile 方法认为它成功了,但在实际的 FTP 上,创建的文件大小为 0KB.

What is happening is that the UploadFile method thinks it succeeded, but on the actual FTP, the file is created with size 0KB.

请问我做错了什么?我也尝试添加缓冲区大小,但没有用.

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

我在网上找到了代码.我应该做这样的事情吗:

I found code on the web. Should I be doing something like this:

client.ChangeDirectory(pFileFolder);
client.Create(pFileName);
client.AppendAllText(pFileName, pContents);

推荐答案

写入流后,流指针位于流的末尾.因此,当您将流传递给 .UploadFile 时,它会从指针(位于末尾)到末尾读取流.因此,什么都没有写.并且不会发出任何错误,因为一切都按设计运行.

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.

在将流传递给 .UploadFile 之前,您需要将指针重置为开头:

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)


另一种方法是使用 SSH.NET PipeStream,它具有单独的读写指针.


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

这篇关于使用 SSH.NET 从 ByteArray/MemoryStream 上传 - 文件创建为空(大小为 0KB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)
Grouping Contiguous Dates(分组连续日期)
SQLite.net SQLiteFunction not working in Linq to SQL(SQLite.net SQLiteFunction 在 Linq to SQL 中不起作用)
LINQ to SQL and Null strings, how do I use Contains?(LINQ to SQL 和空字符串,我如何使用包含?)