none
How to use WebClient to FTP Uploadfile to the Root directory of the FTP Server? RRS feed

  • Question

  • I have not bee able to figure this one out for a few days.


    I have a FTP server which when the User logs in they are immediately placed in their "pickup folder".  This is how the Host is configured. ie: ftp:\\ftp.mysite.com\USERNAME\

    I need to be able to upload to ftp:\\ftp.mysite.com\UPLOAD

    The FTP user has access to ftp:\\ftp.mysite.com and ftp:\\ftp.mysite.com\UPLOAD  So I am certain it is not a permission thing.  I also know it works because I can navigate to this folder via Internet Explorer and FileZilla interfaces.

    Basically I need to be able to perform a "CD.." (or something like this) So that I can go to the FTP ROOT directory to perform my upload. 
    I currently get a 550 Error because it is looking for ftp:\\ftp.mysite.com\UPLOAD, which that directory do not exist on the initial user login.

            Try
                Dim FTPUpReq As FtpWebRequest = WebRequest.Create("ftp://" & ftpuri & "/../UPLOAD/")
                FTPUpReq.Credentials = New Net.NetworkCredential(ftpusername, ftppassword)
                FTPUpReq.Method = WebRequestMethods.Ftp.UploadFile
                Using ftpClient As New WebClient()
                    ftpClient.Credentials = New System.Net.NetworkCredential(ftpusername, ftppassword)               
                    ftpClient.UploadFile("ftp://" & ftpuri & "/UPLOAD/test.txt", strFullLocalFilePath2Upload)
                End Using
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try


    Thursday, January 17, 2019 9:17 PM

All replies

  • instead of 

     ftpClient.UploadFile("ftp://" & ftpuri & "/UPLOAD/test.txt", strFullLocalFilePath2Upload)

    try it like

     ftpClient.UploadFile("ftp://" & ftpuri & "/../UPLOAD/test.txt", strFullLocalFilePath2Upload)

    Friday, January 18, 2019 1:48 AM
  • Hi,

    You can try my code,it works well

     Public Shared Function ftp(ByVal filename As String) As Integer
            Dim ftpServerIP As String = "xxxxx" 'IP address
            Dim ftpUserID As String = "xxxx"
            Dim ftpPassword As String = "xxxxx"
            Dim fileInfo As FileInfo = New FileInfo(filename)
            Dim uri As String = "ftp://" & ftpServerIP & "/" & fileInfo.Name
            Dim reqFTP As FtpWebRequest
    
            Try
                reqFTP = CType(FtpWebRequest.Create(New Uri("ftp://" & ftpServerIP & "/" & fileInfo.Name)), FtpWebRequest)
                reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword)
                reqFTP.KeepAlive = False
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile
                reqFTP.UseBinary = True
            Catch
                Return 0
            End Try
    
            reqFTP.ContentLength = fileInfo.Length
            Dim buffLength As Integer = 2048
            Dim buff As Byte() = New Byte(buffLength - 1) {}
            Dim contentLen As Integer
            Dim fs As FileStream = fileInfo.OpenRead()
    
            Try
                Dim strm As Stream = reqFTP.GetRequestStream()
                contentLen = fs.Read(buff, 0, buffLength)
    
                While contentLen <> 0
                    strm.Write(buff, 0, contentLen)
                    contentLen = fs.Read(buff, 0, buffLength)
                End While
    
                strm.Close()
                fs.Close()
                Dim response As FtpWebResponse = CType(reqFTP.GetResponse(), FtpWebResponse)
                response.Close()
                Return 0
            Catch
                fs.Close()
                fs.Dispose()
                Return 1
            End Try
        End Function

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 18, 2019 2:53 AM
  • I have already tried this, it fails (550 error).  Though it works in the browser window, just not from code.
    Friday, January 18, 2019 1:45 PM
  • I am going to give this a try this morning and will post my results.
    Friday, January 18, 2019 1:46 PM
  • I am getting the 550 error still.

    Dim uri As String = "ftp://" & ftpServerIP & "/" & fileInfo.Name
            Dim reqFTP As FtpWebRequest

            Try
                reqFTP = CType(FtpWebRequest.Create(New Uri("ftp://" & ftpServerIP & "/../UPLOAD/" & fileInfo.Name)), FtpWebRequest)
                reqFTP.Credentials = New NetworkCredential(ftpUserID, ftpPassword)
                reqFTP.KeepAlive = False
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile
                reqFTP.UseBinary = True
            Catch ex As Exception
                MsgBox(ex.Message)
                Return 0
            End Try


    Any suggestions?  

    I am able to UPLOAD directly into the initial FTP directory without a problem.  I cant seem to "move" up one directory so that I can find the UPLOAD folder.
    Friday, January 18, 2019 2:23 PM
  • Hi,

    see the following link:

    http://nickstips.wordpress.com/2010/10/25/c-ftp-upload-error-the-remote-server-returned-an-error-550-file-unavailable-e-g-file-not-found-no-access/

    https://www.dreamincode.net/forums/topic/76361-file-upload-to-server/

    http://forums.asp.net/t/1374306.aspx/1

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, January 21, 2019 8:20 AM