locked
Upload file to a FTP server in the folder other than the default folder RRS feed

  • Question

  • User-1735938375 posted

    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

    Sunday, October 9, 2011 11:43 PM

All replies

  • User-837620913 posted

    Set the ftp file path in the constructor when you call Create:

    WebRequest myWebRequest = WebRequest.Create("ftp://myFTPserver.com/Go.Data72.CLEVDATA/"); 
    Monday, October 10, 2011 6:19 AM
  • User-1735938375 posted

    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 Try

    Thanks,
    Lakhan

    Monday, October 10, 2011 7:44 AM