InputStream

Top  Previous  Next

You can download a file to an FTP server using GetInputStream method.

 

 

[C#]

// create new Ftp instance

Ftp ftp = new Ftp(hostname,username,password,port);        

 

// establish connection

ftp.Connect();

 

// get the InputStream from the data connection

Stream inputStream = ftp.GetInputStream( "C:\\file.txt", 0 );

// create the file

Stream outputStream = File.Create( "C:\\file.txt" );

 

// an array of bytes

byte[] buffer = new byte[ 8192 ];

// the total number of bytes read into the buffer

int read = 0;

                 

// reads a sequence of bytes from the current file

while( ( read = inputStream.Read( buffer, 0, buffer.Length ) ) != 0 ) {

 // writes a sequence of bytes to the current stream

 outputStream.Write( buffer, 0, read );

}

 

// closes the current streams

inputStream.Close();

outputStream.Close();

 

// disconnect

ftp.Disconnect();

 

 

 

[Visual Basic]

' create new Ftp instance

Dim ftp As Ftp = New Ftp(hostname, username, password, port)

 

' establish connection

ftp.Connect()

 

' get the InputStream from the data connection

Dim inputStream As Stream = ftp.GetInputStream("C:\file.txt", 0)

' create the file

Dim outputStream As Stream = System.IO.File.Create("C:\file.txt")

 

' an array of bytes

Dim buffer() As Byte = New Byte(8192) {}

' the total number of bytes read into the buffer

Dim read As Integer = 0

 

 

While True

 ' reads a sequence of bytes from the current file

 read = inputStream.Read(buffer, 0, buffer.Length)

 If read <> 0 Then

         ' writes a sequence of bytes to the current stream

         outputStream.Write(buffer, 0, read)

 Else

         Exit While

 End If

End While

 

' closes the current streams

inputStream.Close()

outputStream.Close()

 

' disconnect

ftp.Disconnect()