Results 1 to 2 of 2

Thread: get FTP file upload completed properly

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2013
    Posts
    285

    get FTP file upload completed properly

    I am using below code to upload files to web server FTP . In the below code i am facing some problems. I want the file to be completely uploaded and then control should come out of this function. Now in some cases control comes out of this function when some connection error occurs etc.I get error in catch .But even though error catches upload sometimes continues. So i want to know whether upload completed completely and then only come out of this function. How i can do it?

    I tried checking file size of the uploading file using Ftp.GetFileSize .but even after all bytes uploaded it returns "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)." error.

    So how i can check file uploaded completely?

    Code:
    Public Function Upload(ByVal fi As FileInfo) As Boolean
    
            Dim URI As String = Hostname '' website is set here
            'perform copy
            Dim ftp As Net.FtpWebRequest = GetRequest(URI)
    
            'Set request to upload a file in binary
            ftp.Method = Net.WebRequestMethods.Ftp.UploadFile
            ftp.UseBinary = True
    
            'Notify FTP of the expected size
            ftp.ContentLength = fi.Length
    
    
            Const BufferSize As Integer = 400000000
            Dim content(BufferSize - 1) As Byte, dataRead As Integer
    
            'open file for reading 
            Using fs As FileStream = fi.OpenRead()
                Try
                    'open request to send
                    Using rs As Stream = ftp.GetRequestStream
                        Do
                            dataRead = fs.Read(content, 0, BufferSize)
                            rs.Write(content, 0, dataRead)
                        Loop Until dataRead < BufferSize
                        rs.Close()
                    End Using
    
    
                Catch ex As Exception
    
                Finally
                    'ensure file closed
                    fs.Close()
                End Try
    
            End Using
    
            ftp = Nothing
            Return True
    
    End Function

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: get FTP file upload completed properly

    Why do you catch the exception, ignore it and then always return true? I would start by returning false when an exception occurs. How you proceed from that point may depend upon what the exception is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width