OutputStream |
You can upload a file to an FTP server using GetOutputStream method.
[C#]
// create new Ftp instance
Ftp ftp = new Ftp(hostname,username,password,port);
// establish connection
ftp.Connect();
// create new System.IO.FileInfo instance
FileInfo fileInfo = new FileInfo( "C:\\FileToUpload.txt" );
// opens the file to upload
Stream inputStream = fileInfo.OpenRead();
// get the OutputStream from the data connection
Stream outputStream = ftp.GetOutputStream( fileInfo.Name, false, 0 );
// 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()
' create new System.IO.FileInfo instance
Dim fileInfo As FileInfo = New FileInfo("C:\FileToUpload.txt")
' opens the file to upload
Dim inputStream As Stream = fileInfo.OpenRead()
' get the OutputStream from the data connection
Dim outputStream As Stream = ftp.GetOutputStream(fileInfo.Name, False, 0)
' 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()