# vb.net ftp transfer



## shuggans (Jan 21, 2012)

Hello all,
I'm working on an app that uses FTP to transfer a file to a server, however, currently writes the file to memory while sending.  It also never releases the memory it used to send.  Is there any way to break the file into chunks as it sends?  App becomes unstable when used with large files.


```
Do Until sent = True
            Try
                Dim FiletoSend() As Byte = System.IO.File.ReadAllBytes(SendFile)
                Dim SendFileStream As System.IO.Stream = FTPRequest.GetRequestStream()
                My.Settings.Status = "Uploading File..."
                My.Settings.Save()
                TrayIcon.BalloonTipText = My.Settings.Status
                If TrayIcon.Visible = True Then
                    TrayIcon.ShowBalloonTip(5000)
                End If
                SendFileStream.Write(FiletoSend, 0, FiletoSend.Length)
                SendFileStream.Close()
                SendFileStream.Dispose()
                My.Settings.Status = "File Uploaded Successfully."
                My.Settings.Save()
                TrayIcon.BalloonTipText = My.Settings.Status
                If TrayIcon.Visible = True Then
                    TrayIcon.ShowBalloonTip(5000)
                End If
                sent = True
            Catch
                Thread.Sleep(1000)
            End Try
        Loop
    End Sub
```


----------



## FordGT90Concept (Jan 22, 2012)

Change to a FileStream, create a byte array of 2-16 KiB to act as a buffer, read from the FileStream to the buffer, write the buffer to the FTP stream, rince and repeat until there is nothing left to write.


If you don't want to get into the nitty-gritty of buffering and the like, I'd recommend using an already made FTP Client like this one:
http://www.codeproject.com/KB/IP/Ft...pp=25&noise=3&sort=Position&view=Quick&fr=201

.NET only provides low level infrastructure for FTP (like many protocols) so you'd have to author your own or use someone elses to make FTP simple to use.


----------



## shuggans (Jan 22, 2012)

I got it to split into chunks,
however, I noticed the file on the source machine is twice as big as the file that ends up on the ftp server.  Anything wrong stick out to you?       


```
Dim SendFileShort As String = System.IO.Path.GetFileNameWithoutExtension(SentFile)
        Dim FTPRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://" & My.Settings.FTPServer & "/" & SendFileShort), System.Net.FtpWebRequest)
        FTPRequest.Credentials = New System.Net.NetworkCredential(My.Settings.FTPUser, My.Settings.FTPPassword)
        FTPRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
        Dim entered As Boolean = False
        Dim sent As Boolean = False        
Do Until sent = True
            Try
                Dim bytesread As Integer = 0
                Dim buffer As Integer = 2048
                Dim fs As FileStream = File.OpenRead(SentFile)
                Dim byteFile As Byte() = New Byte(2048) {}
                Dim fstream As Stream = FTPRequest.GetRequestStream
                Do
                    bytesread = fs.Read(byteFile, 0, Buffer)
                    fs.Read(byteFile, 0, Buffer)
                    fstream.Write(byteFile, 0, byteFile.Length)
                Loop Until bytesread = 0
                fstream.Close()
                fstream.Dispose()
                My.Settings.Status = "File Uploaded Successfully."
                My.Settings.Save()
                TrayIcon.BalloonTipText = My.Settings.Status
                If TrayIcon.Visible = True Then
                    TrayIcon.ShowBalloonTip(5000)
                End If
                sent = True
            Catch
                Thread.Sleep(1000)
            End Try
        Loop
```


----------



## temp02 (Jan 22, 2012)

Delete this line:

```
fs.Read(byteFile, 0, Buffer)
```


----------



## shuggans (Jan 22, 2012)

Doh!  
Early Morning Programming, not my forte.  Thanks a bunch!


----------



## shuggans (Jan 22, 2012)

*Hmmm*

Well... I sent a few video files through this to test, and they arent exactly the same size as the source file when they get to the server.  Something's being lost. any ideas?


----------



## Kreij (Jan 22, 2012)

Before you close the filestream, flush it.


----------



## shuggans (Jan 22, 2012)

Changed to this: files still arent exactly the same (size is off on the server's copy). am I flushing at the wrong time?  Does it need to be inside the loop?


```
Do Until sent = True
            Try
                Dim bytesread As Integer = 0
                Dim buffer As Integer = 2048
                Dim fs As FileStream = File.OpenRead(SentFile)
                Dim byteFile As Byte() = New Byte(2048) {}
                Dim fstream As Stream = FTPRequest.GetRequestStream
                Do
                    bytesread = fs.Read(byteFile, 0, Buffer)
                    fstream.Write(byteFile, 0, byteFile.Length)
                Loop Until bytesread = 0
                 
                fs.flush()
                fstream.flush()
                fs.close()
                fstream.Close()
                fs.Dispose()
                fstream.Dispose()

                My.Settings.Status = "File Uploaded Successfully."
                My.Settings.Save()
                TrayIcon.BalloonTipText = My.Settings.Status
                If TrayIcon.Visible = True Then
                    TrayIcon.ShowBalloonTip(5000)
                End If
                sent = True
            Catch
                Thread.Sleep(1000)
            End Try
        Loop
```


----------



## FordGT90Concept (Jan 22, 2012)

You should Flush after writing about 4 KiB of data to a stream everytime 4 KiB or less of data is written.  It should be something like (pseudo code)...

```
buffer[4096]

while dataavailable > 0
  buffer = input.read(4096)
  output.write(buffer)
  output.flush()
  dataavialble -= read
end while
```


----------



## temp02 (Jan 22, 2012)

I'm not very familiar with VB.Net, but shouldn't 
	
	



```
fstream.Write(byteFile, 0, byteFile.Length)
```
 be 
	
	



```
fstream.Write(byteFile, 0, bytesread)
```
 ?

I mean, nothing guarantees you that the last chunk of data is (a multiple of) <BufferSize> long.


----------



## shuggans (Jan 22, 2012)

*shuggans*

Nice catch temp.  You are correct.


----------

