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 (51.6k points)
Public Class MemoryTransferExample
   
   Public Sub Run(serverAddress As String, serverPort As Integer, userName As String, password As String, fileName As String)

      ' Create FTPConnection
      Dim ftpConnection As New FTPConnection

      ' setting server address and credentials
      ftpConnection.ServerAddress = serverAddress
      ftpConnection.ServerPort = serverPort
      ftpConnection.UserName = userName
      ftpConnection.Password = password
      
      ' connect to server
      Console.WriteLine("Connecting to server " & serverAddress)
      ftpConnection.Connect()
      
      ' get the current working directory
      Console.WriteLine("Working directory is " & ftpConnection.GetWorkingDirectory())
      
      ' get data to be transferred
      Dim s As String = "Hello world"
      Dim bytes As Byte() = Encoding.ASCII.GetBytes(s)
      
      ' upload the byte-array to a file on the server
      Console.WriteLine("Uploading byte-array to the file '" & fileName & "'")
      ftpConnection.UploadByteArray(bytes, fileName)
      
      ' download byte-array and display it
      Console.WriteLine("Downloading the file '" & fileName & "' into a byte-array")
      bytes = ftpConnection.DownloadByteArray(fileName)
      Console.WriteLine("Downloaded string = " & Encoding.ASCII.GetString(bytes))
      
      ' build StringStream (defined below) for "Hello world"
      Dim inStr As New StringStream(s)
      
      ' upload the stream to a file on the server
      Console.WriteLine("Uploading stream to the file '" & fileName & "'")
      ftpConnection.UploadStream(inStr, fileName)
      inStr.Close()
      
      ' create a StringStream to download into
      Dim outStr As New StringStream()
      Console.WriteLine("Downloading the file '" & fileName & "' into a stream")
      ftpConnection.DownloadStream(outStr, fileName)
      Console.WriteLine("Downloaded string = " & outStr.ToString())
      outStr.Close()
      
      ' Shut down client
      Console.WriteLine("Closing client")
      ftpConnection.Close()
      
      Console.WriteLine("Example complete")
   End Sub 'Run
   
   
   Class StringStream
      Inherits Stream
      Private buffer As MemoryStream
      
      Public Sub New()
         buffer = New MemoryStream()
      End Sub 'New
      
      Public Sub New(str As String)
         buffer = New MemoryStream(Encoding.ASCII.GetBytes(str))
      End Sub 'New
      
      Public Overrides Function Read(bytes() As Byte, offset As Integer, count As Integer) As Integer
         Return buffer.Read(bytes, offset, count)
      End Function 'Read
      
      Public Overrides Sub Write(bytes() As Byte, offset As Integer, count As Integer)
         buffer.Write(bytes, offset, count)
      End Sub 'Write
      
      Public Overrides Function Seek(offset As Long, origin As SeekOrigin) As Long
         Return buffer.Seek(offset, origin)
      End Function 'Seek
      
      Public Overrides Function ToString() As String
         Return Encoding.ASCII.GetString(buffer.GetBuffer())
      End Function 'ToString
      
      Public Overrides ReadOnly Property CanRead() As Boolean
         Get
            Return True
         End Get
      End Property
      
      Public Overrides ReadOnly Property CanWrite() As Boolean
         Get
            Return True
         End Get
      End Property
      
      Public Overrides ReadOnly Property CanSeek() As Boolean
         Get
            Return True
         End Get
      End Property
      
      Public Overrides Sub Flush()
         buffer.Flush()
      End Sub 'Flush
      
      Public Overrides ReadOnly Property Length() As Long
         Get
            Return buffer.Length
         End Get
      End Property
      
      Public Overrides Sub SetLength(value As Long)
         buffer.SetLength(value)
      End Sub 'SetLength
      
      Public Overrides Property Position() As Long
         Get
            Return buffer.Position
         End Get
         Set
            buffer.Position = value
         End Set
      End Property
   End Class 'StringStream
End Class 'MemoryTransferExample


- Hans (EDT)
0 votes
by (380 points)
I'm trying to use the DownloadStream() method in C#, and need to get the data into a StreamReader object. So far I've been unsuccessful. Could you please share how to accomplish this?

Thanks in advance,
Mike
0 votes
by (162k points)
There is very little difference between C# and the VB.NET code shown above.

I'm trying to use the DownloadStream() method in C#, and need to get the data into a StreamReader object. So far I've been unsuccessful. Could you please share how to accomplish this?

Thanks in advance,
Mike
0 votes
by (380 points)
It appears that the VB code requires a wrapper class around the memorystream to use. Would that be required for C# as well? I would have expected something like,

MemoryStream ms = new MemoryStream();
ftpConnection1.DownloadStream(ms, strFileName);
StreamReader sr = new StreamReader(ms);

but this does not succeed. Any thoughts?
0 votes
by (162k points)
MemoryStreams cannot be read from once they are closed, which is what happens internally at the end of the transfer.

Set the CloseStreamsAfterTransfer property to false to allow the stream to stay open for reading after the transfer is complete (and set it back to true afterwards).

This might be the problem ...

It appears that the VB code requires a wrapper class around the memorystream to use. Would that be required for C# as well? I would have expected something like,

MemoryStream ms = new MemoryStream();
ftpConnection1.DownloadStream(ms, strFileName);
StreamReader sr = new StreamReader(ms);

but this does not succeed. Any thoughts?
0 votes
by (380 points)
That sounds like the key. However, I can't seem to locate this property (CloseStreamsAfterTransfer) in the edtftpnet object. Is this in a new version? (I have v1.2.2).
0 votes
by (162k points)
Sorry about that - I don't think it has been released. We are releasing 1.2.3 very soon, but if you want to try a pre-release version email us at support at enterprisedt dot com and we'll send it to you.

That sounds like the key. However, I can't seem to locate this property (CloseStreamsAfterTransfer) in the edtftpnet object. Is this in a new version? (I have v1.2.2).
0 votes
by (380 points)
using v1.2.3...

Tried the CloseStreamsAfterTransfer as suggested. Code snippet as follows:

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);
ftpConnection1.CloseStreamsAfterTransfer = true;


I have been successful at using the DownloadArray() method, as follows

byte[] bytes;
bytes = ftpConnection1.DownloadByteArray("98320611.TSX");
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
System.IO.StreamReader sr = new System.IO.StreamReader(ms);

Any thoughts as to why the DownloadStream() fails?
0 votes
by (162k points)
So what error did you receive? Can you post a snippet of the log file?

using v1.2.3...

Tried the CloseStreamsAfterTransfer as suggested. Code snippet as follows:

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);
ftpConnection1.CloseStreamsAfterTransfer = true;

0 votes
by (380 points)
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?

Categories

...