使用SSH.NET从ByteArray / MemoryStream上传-文件创建为空(大小为0KB) [英] Upload from ByteArray/MemoryStream using SSH.NET - File gets created empty (with size 0KB)

查看:73
本文介绍了使用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

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从ByteArray / MemoryStream上传-文件创建为空(大小为0KB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆