Need help with FTP (download and upload)

UNIX

Member
Joined
Feb 28, 2008
Messages
10
Programming Experience
Beginner
Hello,
i need three functions which can do the following:
1) Establish Connection to a server with username and password
2) Download a File
3) Upload a File

Optional: Resume File-Download

Can someone please help me?

regards,
UNIX
 
Four options ranked by simplicity:
  1. My.Computer.Network.DownloadFile/UploadFile
  2. WebClient
  3. FtpWebRequest
  4. implementing a Ftp client with sockets, ie TcpClient (+TcpListener for active)
 
Hello JohnH,

thanks for your answer. I used the My.Computer.Network.DownloadFile/UploadFile method, which works very well.
Now I need a method to resume a filedownload. Can you please help me?

I also need a function, which establish a connection to my ftp server and controlls every 1000ms (with a timer) if it is still established or not.
Can you help me?
 
Last edited:
You can do this by specifying the byte offset in ContentOffset property of FtpWebRequest.
 
Hi JohnH,

i read on the msdn site about the components you wrote, but i dont understand, how it acutally works. Can you please explain it to me or give a code example?
 
VB.NET:
Public Sub ResumeFTP(ByVal url As Uri, _
                     ByVal filename As String, _
                     ByVal cred As Net.NetworkCredential, _
                     ByVal offset As Long)

    Dim req As Net.FtpWebRequest = Net.FtpWebRequest.Create(url)
    req.Credentials = cred
    req.ContentOffset = offset
    Dim res As Net.FtpWebResponse = req.GetResponse
    If res.StatusCode = Net.FtpStatusCode.OpeningData Then
        Dim reStream As IO.Stream = res.GetResponseStream
        Dim fs As New IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Write)
        fs.Position = offset
        CopyStream(reStream, fs)
        fs.Close()
        reStream.Close()
    End If
    res.Close()
End Sub

Public Sub CopyStream(ByVal source As IO.Stream, ByVal destination As IO.Stream)
    Dim read As Integer = -1
    Dim buffer(8191) As Byte
    While read <> 0
        read = source.Read(buffer, 0, buffer.Length)
        destination.Write(buffer, 0, read)
    End While
End Sub
Usage sample, for example you have a file that is downloaded 98304 bytes:
VB.NET:
Dim url As New Uri("ftp://server/filename.ext")
Dim file As String = IO.Path.Combine(Application.StartupPath, "filename.ext")
Dim cred As New Net.NetworkCredential("username", "password")
ResumeFTP(url, file, cred, 98304)
Notice you can use the same parameters for the My..DownloadFile method. For example start this and cancel it, find the file byte count downloaded so far and resume from there with the above example.
VB.NET:
My.Computer.Network.DownloadFile(url, file, cred, True, 100, True, UICancelOption.DoNothing)
 
Hi JohnH,

thank you very much for your help. I still have some problems; can i somehow send you my project, so that you can see it? It still is not working and i dont really know, how to fix it or what i made wrong.

regards,
UNIX
 
You can upload it here as attachment (or link to file on your own site or to any public file share service). Remember to remove the Bin and Obj folder from the zip. Describe what is the problem.
 
Hi,
VB.NET:
  Private Sub download()
        Try
            Dim Zielquelle As String = "E:\video.mpg"
            Dim Zieldatei As String = "lionseul.mpg"
            Dim cred As New Net.NetworkCredential("user", "pass!")
            Dim Server1 As New Uri(Server & Zieldatei)
            My.Computer.Network.DownloadFile(Server1, Zielquelle, User, Passwort, False, 99999, True)
        Catch ex As Exception

        End Try
    End Sub

Using this Code, i download a file which is working. When i then remove the network cable out of my pc, the program should then recognize this. when the cable is then again connected (i am testing this with a normal network cable. connection loss and reconnecting should simulate wlan), it should resume the download.

A problem which also occurs then, is that if the connection ist lost for a few seconds, the file i downloaded so far, will be deleted.
 
Last edited:
that if the connection ist lost for a few seconds, the file i downloaded so far, will be deleted.
Are you sure about that? When I do the same the partial downloaded file remains. Anyway you can can only resume if there is anything left from first download.
 
How long did you remove the cable? If it is only for ~three seconds or less, than it works for me fine. But if i remove it for ~five or more seconds, the so-far downloaded file will be deleted automatically.

Is it maybe conditioned to the Operating System used?

What else could be the reason? Could you maybe send me your project or attach it, so that i can compare it with mine? Maybe yours is a little different.

It tried it several times, and always the same :(
I also varied the parameter for the server timeout, but that also didn't help.
 
How long did you remove the cable?
Permanently, as far as the application run concerns.
Is it maybe conditioned to the Operating System used?
I don't think so. Regardless of OS the same code is run by the same Framework version.
What else could be the reason?
I have no idea.
Could you maybe send me your project or attach it
:cool:
VB.NET:
try
My.Computer.Network.DownloadFile(params...)
catch
 
Hi JohnH,

i really hope that you can help me now. solving my problem would be great.

i changed username, password and server, but you can see my code.

pw for the file is: abc123

VB.NET:
http://rapidshare.com/files/100292262/FTPDownload.rar.html
 
What is the problem?
 
The same as before; the so far downloaded file will be deleted. Can you please try it with my project file, if it still works when you run it or do you have then the same problem?
which operating system do you use?

Is it possible, that it depends on the server, from where i want to download the files?
 
Back
Top