Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
13.3k views
in .NET FTP by (51.6k points)
An edtFTPnet user asks:
Would you please give me an example of how to use the function FTPConnection.DownloadStream in VB.NET?

16 Answers

0 votes
by (162k points)
Looks ok. Will try the code out myself & report back ....

Perhaps I should have been more explicit -

the download does not FAIL, but the resulting stream is empty. Are the streams being used (in the code provided) as you intended?
0 votes
by (380 points)
I think perhaps the problem is with my usage of the streams. I used the following code,

ftpConnection1.CloseStreamsAfterTransfer = false;
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ftpConnection1.DownloadStream(ms, "98320611.TSX");
System.IO.StreamReader sr = new System.IO.StreamReader(ms);
byte[] bytes;
bytes = ms.GetBuffer();
ftpConnection1.CloseStreamsAfterTransfer = true;

and I then inspect the bytes[] buffer, and all data is present. Therefore, I'm convinced that edtftp is downloading to the stream correctly, apparently I'm just failing to get the data from the MemoryStream to the StreamReader properly. As I mentioned, I'd love to simply download the stream directly into the StreamReader if possible. If you have any thoughts on this, please post.

Thanks!
0 votes
by (51.6k points)
You'll probably find that the Position "pointer" of the MemoryStream is at the end of the stream after the DownloadStream method completes. You need to reset it back to beginning before you read from it. In other words:
ftpConnection1.DownloadStream(ms, "98320611.TSX");
ms.Seek(0, System.IO.SeekOrigin.Begin);


- Hans (EDT)
0 votes
by (380 points)
Thanks very much, that was the key. The resulting code, for other folks that might want it, is
ftpConnection1.CloseStreamsAfterTransfer = false;
MemoryStream ms = new MemoryStream();
ftpConnection1.DownloadStream(ms, myFileName);
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
... use sr for whatever ...
0 votes
by (140 points)
Hi there,

Is there any way I can get the version that has the method CloseStreamAfterTransfer?

I can't seem to access the stream because it says it is closed.

Thanks.
0 votes
by (162k points)
The latest version, 1.2.3, has this feature.

Hi there,

Is there any way I can get the version that has the method CloseStreamAfterTransfer?

I can't seem to access the stream because it says it is closed.

Thanks.

Categories

...