I am facing problem to upload the file to FTP server.
As ftp server is : ftp://myFTPserver.com
Current Default Folder: GO.$DataW1.SVMANNET
But I want to upload the file on Go.Data72.CLEVDATA Folder.
Can you please suggest me how to change the path from teh default folder to teh folder where i want to upload the files.
Like from command Prompt we can use ftp> cd \GO.$DATA72.CLEVDATA to change the current directory. how can we achieve the same in dot net.
Dim ftpUsername As String
Dim ftpPassword As String
Dim credential As NetworkCredential
Dim request As FtpWebRequest
Dim reader As FileStream
Dim stream As Stream
Dim response As FtpWebResponse
Dim Source As String="C:\Lakhan\abc.txt"
Dim Target As String = "ftp://myFtpServer/GO.$DATA72.CLEVDATA/abc.txt"
Try
ftpUsername = ConfigurationManager.AppSettings("FTPUsername").ToString()
ftpPassword = ConfigurationManager.AppSettings("FTPPassword").ToString()
'Upload file on FTP server
'My.Computer.Network.UploadFile(Source, Target, ftpUsername, ftpPassword)
credential = New NetworkCredential(ftpUsername, ftpPassword)
request = DirectCast(WebRequest.Create(Target), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = credential
reader = New FileStream(Source, FileMode.Open)
Dim buffer(Convert.ToInt32(reader.Length - 1)) As Byte
reader.Read(buffer, 0, buffer.Length)
reader.Close()
request.ContentLength = buffer.Length
stream = request.GetRequestStream
stream.Write(buffer, 0, buffer.Length)
stream.Close()
response = DirectCast(request.GetResponse, FtpWebResponse)
response.Close()
Catch ex As Exception
'Throw the exception to skip the further processing of this file
Throw New Exception(String.Concat(New String() {"Error in UploadFileToFTP: ", ex.Message}))
End Try
Lakhan Pal
Member
2 Points
2 Posts
Upload file to a FTP server in the folder other than the default folder
Oct 10, 2011 03:43 AM|LINK
Hi,
I am facing problem to upload the file to FTP server.
As ftp server is : ftp://myFTPserver.com
Current Default Folder: GO.$DataW1.SVMANNET
But I want to upload the file on Go.Data72.CLEVDATA Folder.
Can you please suggest me how to change the path from teh default folder to teh folder where i want to upload the files.
Like from command Prompt we can use ftp> cd \GO.$DATA72.CLEVDATA to change the current directory. how can we achieve the same in dot net.
Thanks,
Lakhan
DarrellNorto...
All-Star
86555 Points
9624 Posts
Moderator
MVP
Re: Upload file to a FTP server in the folder other than the default folder
Oct 10, 2011 10:19 AM|LINK
Set the ftp file path in the constructor when you call Create:
WebRequest myWebRequest = WebRequest.Create("ftp://myFTPserver.com/Go.Data72.CLEVDATA/");Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
Lakhan Pal
Member
2 Points
2 Posts
Re: Upload file to a FTP server in the folder other than the default folder
Oct 10, 2011 11:44 AM|LINK
I am using the following code. I have already try to pass ftp://myFTPserver.com/Go.Data72.CLEVDATA/ to constructor, its is not working.
Dim ftpUsername As String Dim ftpPassword As String Dim credential As NetworkCredential Dim request As FtpWebRequest Dim reader As FileStream Dim stream As Stream Dim response As FtpWebResponse Dim Source As String="C:\Lakhan\abc.txt" Dim Target As String = "ftp://myFtpServer/GO.$DATA72.CLEVDATA/abc.txt" Try ftpUsername = ConfigurationManager.AppSettings("FTPUsername").ToString() ftpPassword = ConfigurationManager.AppSettings("FTPPassword").ToString() 'Upload file on FTP server 'My.Computer.Network.UploadFile(Source, Target, ftpUsername, ftpPassword) credential = New NetworkCredential(ftpUsername, ftpPassword) request = DirectCast(WebRequest.Create(Target), FtpWebRequest) request.Method = WebRequestMethods.Ftp.UploadFile request.Credentials = credential reader = New FileStream(Source, FileMode.Open) Dim buffer(Convert.ToInt32(reader.Length - 1)) As Byte reader.Read(buffer, 0, buffer.Length) reader.Close() request.ContentLength = buffer.Length stream = request.GetRequestStream stream.Write(buffer, 0, buffer.Length) stream.Close() response = DirectCast(request.GetResponse, FtpWebResponse) response.Close() Catch ex As Exception 'Throw the exception to skip the further processing of this file Throw New Exception(String.Concat(New String() {"Error in UploadFileToFTP: ", ex.Message})) End TryThanks,
Lakhan