Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

Am working on a project where I need to download an attachment from a physical path using FTP. When am using Filezilla client , its downloading correctly.Also I am able to download file using FTP commands. But When I tried access file and download using code its giving error as "The remote server returned an error: (550) File unavailable (e.g., file not found, no access).". Please help me to resolve this issue. I have written code as follows:

Am passing directory name where file located and FileNM is attachment which I need to download and appending to FTP path . code is as follows:

Private Sub DownloadFTP(ByVal dirName As String, ByVal FileNM As String)
Dim uriPath As String = String.Empty
Dim serverUri As Uri = Nothing
Dim reqFTP As FtpWebRequest = Nothing
Dim response As FtpWebResponse = Nothing
Dim responseStream As Stream = Nothing
Dim writeStream As FileStream = Nothing
Dim localdestn As String = "c:\dir"
Dim bufferSize As Integer = 4096
Dim Length As Integer = 2048
Dim buffer() As Byte = New Byte((Length)) {}
Dim bytesRead As Integer = 0

Try

uriPath = "ftp://itsusmp/DEFAULT/" & dirName & "/" & FileNM
serverUri = New Uri(uriPath)
If serverUri.Scheme <> Uri.UriSchemeFtp Then
Return
End If
reqFTP = DirectCast(FtpWebRequest.Create(New Uri(uriPath)), FtpWebRequest)
reqFTP = CType(FtpWebRequest.Create(uriPath), FtpWebRequest)
reqFTP.Credentials = New NetworkCredential("adminusre", "Pwd@2014")
Dim auth As String = reqFTP.AuthenticationLevel
reqFTP.KeepAlive = False
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
reqFTP.UseBinary = True
reqFTP.Proxy = Nothing
reqFTP.UsePassive = False
response = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
responseStream = response.GetResponseStream()
writeStream = New FileStream(localdestn & "\" & FileNM, FileMode.Create)
bytesRead = responseStream.Read(buffer, 0, Length)
While bytesRead > 0
writeStream.Write(buffer, 0, bytesRead)
bytesRead = responseStream.Read(buffer, 0, Length)
End While
writeStream.Close()
response.Close()

Catch webex As WebException
Throw webex
Catch ex As Exception
Throw ex
End Try
End Sub

Please help me where am going wrong. Thanks in advance.

Thanks
TheRisingSun
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900