• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

vb.net ftp transfer

Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
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.

Code:
        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
 
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.
 
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?

Code:
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
 
Doh! :banghead:
Early Morning Programming, not my forte. Thanks a bunch! :toast:
 
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?
 
Before you close the filestream, flush it.
 
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?

Code:
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
 
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)...
Code:
buffer[4096]

while dataavailable > 0
  buffer = input.read(4096)
  output.write(buffer)
  output.flush()
  dataavialble -= read
end while
 
Last edited:
I'm not very familiar with VB.Net, but shouldn't
Code:
fstream.Write(byteFile, 0, byteFile.Length)
be
Code:
fstream.Write(byteFile, 0, bytesread)
?

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

Nice catch temp. You are correct.
 
Back
Top