I searched and found a lot of codes
and I made a code for myself for transfering file through ftp protocole
but I got some errors there that I dont know:-s

this is my code :

 Dim FTPRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://ftp.microsoft.com/Softlib/" & "README.TXT"), System.Net.FtpWebRequest)
        FTPRequest.Credentials = New System.Net.NetworkCredential("", "")
        FTPRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

        Dim stream As System.IO.Stream = FTPRequest.GetRequestStream
        ' Get the length of the content
        Dim length As Integer = FTPRequest.ContentLength

        ' Set the maximum length of the progress bar.
        ProgressBar1.Maximum = length
        ' Create a temporary array for the content of the file.
        Dim bytes(length) As Byte
        ' Get all bytes of the content and advance the progress bar.
        For i As Integer = 0 To length - 1
            bytes(i) = stream.ReadByte()
            ProgressBar1.Value = i
            Label1.Text = i.ToString + "Bytes Downloaded"
        ' Write the content to the output file.
        Using output As IO.Stream = System.IO.File.Create("C:\Users\MeSam\desktop\file2.txt")
            output.Write(bytes, 0, bytes.Length)
        End Using

can you help me ?

Recommended Answers

All 7 Replies

First, use Try...Catch...End Try to catch the error. It helps to solve the problem. And secondly, everything you do with the web is always more or less error prone.

Basic problem in your code is understanding how communication works between server and the client. In the code above you request something from the server and the server sends you a response.

I dropped the progressbar from your code because ContentLength does not return the filesize (actually reading ContentLength causes an error). You should request Ftp.GetFileSize and read the response. Second change I made is reading incoming data in a blocks and dumping blocks to a file. Reading a large file one byte at a time can be really slow. Anyway, here's my code:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim buffer(1023) As Byte ' Allocate a read buffer of 1kB size
    Dim bytesIn As Integer ' Number of bytes read to buffer
    Dim totalBytesIn As Integer ' Total number of bytes received (= filesize)
    Dim output As IO.Stream ' A file to save response

        Dim FTPRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://ftp.microsoft.com/Softlib/" & "README.TXT"), System.Net.FtpWebRequest)

        ' No credentials needed in this case. Usually you need to provide them. Catch the appropriate error if/when credentials are wrong!
        FTPRequest.Credentials = New System.Net.NetworkCredential("", "")
        ' Send a request to download a file
        FTPRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
        ' FTP server return a _response_ to your request
        Dim stream As System.IO.Stream = FTPRequest.GetResponse.GetResponseStream

        ' If you need the length of the file, send a request Ftp.GetFileSize and read the response
        'Dim length As Integer = CInt(FTPRequest.GetResponse.ContentLength)

        ' Write the content to the output file
        output = System.IO.File.Create("C:\temp\file2.txt")
        bytesIn = 1 ' Set initial value to 1 to get into loop. We get out of the loop when bytesIn is zero
        Do Until bytesIn < 1
            bytesIn = stream.Read(buffer, 0, 1024) ' Read max 1024 bytes to buffer and get the actual number of bytes received
            If bytesIn > 0 Then
                ' Dump the buffer to a file
                output.Write(buffer, 0, bytesIn)
                ' Calc total filesize
                totalBytesIn += bytesIn
                ' Show user the filesize
                Label1.Text = totalBytesIn.ToString + " Bytes Downloaded"
            End If
        ' Close streams
    Catch ex As Exception
        ' Catch exceptions. THIS IS ONLY FOR DEBUGGING ERRORS. In the production code, use a bit smarter exception catching ;)
    End Try

End Sub

Sorry about formatting. I'm used to old bbcodes, read formatting help but still couldn't get code color-coded...


that worked nicely ,thank you
your formatting was ok it was also colored

I still have one more question
Can I get a file from another computer with its ip through internet ?
I mean not local

anyway thank you very much ;)

when I want to upload a file I get this error :

System.Net.WebException:The remote server returned an error: (550) File unavailable

this is my code below :

            Dim oFile As New FileInfo(fileName)
            Dim fileContents As Byte() = New Byte(oFile.Length - 1) {}
            Using fr As FileStream = oFile.OpenRead()
                fr.Read(fileContents, 0, Convert.ToInt32(oFile.Length))
            End Using
            Using writer As Stream = FTPRequest.GetRequestStream
                writer.Write(fileContents, 0, fileContents.Length)
            End Using

I think you get that error when the FTP client does not have Write Permissions to the folder. So you send the file, then the ftp client request that the uploade was successful, the server, says "What file?"

Yes I found my problem
It was in ftprequest I should define my destination file name :D

how to display a prompt dialogbox when I downlaod a file from FTP??

Please do not hijack threads. This is a new question so start a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.