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
 
Well, all your code does it to call My.Computer.Network.DownloadFile. I've already done that and answered you.
 
So the code itself should work. The operating system should also not be the problem.

So its maybe the servers fault? Is it possible, that you can edit the servers user timeout time or something similar? For example, if a user has lost the connection to the server, there is a default time. when this timelimit is exceeded, the user has to reconnect, before he can do anything again? Or isnt there such an option?
 
Can you please explain me, what exactly the two functions do?

Would be great, because i dont understand it very well. How does it work, what does each line?
 
commented code:
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) [COLOR="Green"]'creates webrequest[/COLOR]
    req.Credentials = cred [COLOR="Green"]'assigns credentials[/COLOR]
    req.ContentOffset = offset [COLOR="green"]'set byte offset where to start downloading[/COLOR]
    Dim res As Net.FtpWebResponse = req.GetResponse [COLOR="green"]'sends request and gets response[/COLOR]
    If res.StatusCode = Net.FtpStatusCode.OpeningData Then [COLOR="green"]'checks response status code[/COLOR]
        Dim reStream As IO.Stream = res.GetResponseStream [COLOR="green"]'gets the data stream of response[/COLOR]
        Dim fs As New IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Write) [COLOR="green"]'opens a file stream for write access[/COLOR]
        fs.Position = offset [COLOR="green"]'set byte offset where to start writing[/COLOR]
        CopyStream(reStream, fs) [COLOR="green"]'copies data from one stream to another[/COLOR]
        fs.Close() [COLOR="green"]'close file stream[/COLOR]
        reStream.Close() [COLOR="green"]'close response stream[/COLOR]
    End If
    res.Close() [COLOR="green"]'close response[/COLOR]
End Sub

Public Sub CopyStream(ByVal source As IO.Stream, ByVal destination As IO.Stream)
    Dim read As Integer = -1 [COLOR="Green"]'tracker variable[/COLOR]
    Dim buffer(8191) As Byte [COLOR="green"]'byte buffer array[/COLOR]
    While read <> 0 [COLOR="green"]'while something was read[/COLOR]
        read = source.Read(buffer, 0, buffer.Length) '[COLOR="green"]try to read 8192 bytes from source[/COLOR]
        destination.Write(buffer, 0, read) [COLOR="green"]'write number of bytes read to destination[/COLOR]
    End While
End Sub
Everything here; every class, property, method and keyword used is easy to find and read about in documentation.
 
Back
Top