Question How to FTP upload a file to a subdirectory?

lovepanda

Member
Joined
Apr 26, 2011
Messages
6
Programming Experience
1-3
Hi all,

I am using the below code to upload a file to FTP server.
It will hit error 'The remote server returned an error: (550) File unavailable (e.g., file not found, no access).' when i try to FTP to subdirectory. no problem directly ftp to the root directory.
Can anyone help to advise how to FTP to a sub directory? or suggest a free ftp class?
Thanks a lot.

Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://" & FTPHost & "/" & sFileName), System.Net.FtpWebRequest)
clsRequest.Credentials =
New System.Net.NetworkCredential(FTPID, FTPPW)
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
clsRequest.UsePassive =
False
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
clsRequest.UsePassive =
False
For Each File In files
sFileName = System.IO.Path.GetFileName(File.ToString)

Dim bFile() As Byte = System.IO.File.ReadAllBytes(LocalFilePath & "\" & sFileName)
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()
Next
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
My.Computer.Network.UploadFile is simpler to use than FtpWebRequest class for such default operation.
Log in with any Ftp client that has a user interface and see how it looks in there, make sure you have the folder structure right. The .Net FTP tools is no different than using any other FTP client in that regard.
 

lovepanda

Member
Joined
Apr 26, 2011
Messages
6
Programming Experience
1-3
Hi,
With My.Computer.Network.UploadFile , i can only upload file to the root folder. when i try to upload a file to a subdirectory, i will get The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
i can upload the file to the sub directory with same Id and password via external FTP client.Any ideas? Thanks.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,439
Location
Norway
Programming Experience
10+
That means directory you are trying to upload to don't exist, either because you haven't created it and expect the Upload method to so for you (which it doesn't) or that you are not specifying the correct path.
To create a Ftp directory programmatically you have to use the FtpWebRequest class and set Method to value WebRequestMethods.Ftp.MakeDirectory.
 
Top Bottom