开发者

Upload file via FTP - Server returned error (550) File is unavailable, cannot find file

I'm trying to upload a file via FTP from my local computer to an FTP server, which is also on my local computer at the moment. I've got this sub I'm calling:

    Public Sub UploadFTPFile(ByVal ftpservername, ByVal fullfilepath, ByVal filename, ByVal username, ByVal password)

    Dim clsRequest As System.Net.FtpWebRequest = _
    DirectCast(System.Net.WebRequest.Create("ftp://" & ftpservername & "/" & filename), System.Net.FtpWebRequest)
    clsRequest.Credentials = New System.Net.NetworkCredential(username, password)
    clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

    clsRequest.KeepAlive = False

    ' read in file...
    Dim bFile() As Byte = System.IO.File.ReadAllBytes(fullfilepath)

    ' upload file...
    Dim clsStream As System.IO.Stream = _
        clsRequest.GetRequestStream()
    clsStream.Write(bFile, 0, bFile.Length)
    clsStream.Close()
    clsStream.Dispose()
End Sub

When calling the sub, I do it like this:

UploadFTPFile("192.168.1.3/Temp", selectedSoundFileLong, OpenFileDialog.SafeFileName, "", "")

In other words, in the Sub, this string:

DirectCast(System.Net.WebRequest.Create("ftp://" & ftpservername & "/" & filename), System.Net.FtpWebRequest)

, creates the following:

    DirectCast(System.Net.WebRequest.Create("ftp://192.168.1.3/Temp/test.mp3"), System.Net.FtpWebRequest)

And at this line in the sub:

        D开发者_运维百科im clsStream As System.IO.Stream = _
        clsRequest.GetRequestStream()

This error occurs:

The remote server returned an error: (550) File unavailable

What could be the cause of this? I'm running an FTP Server using Golder FTP Server, which is freeware. I think it's setup correctly because connecting to the FTP Server using the exact same string as above using Windows Explorer works great.


are you sure there is no extra white space in the webRequest string? I believe you would get this error if the string happened to be like "ftp://192.168.1.3/ Temp/test.mp3" Also, check to make sure you have the correct privileges to write to that server. Additionally, make sure you file is test.mp3 and not test.MP3.


Try flipping the UsePassive property. Control and data use different ports in FTP, it's possible you're getting through on the control port but getting blocked somehow on the data port.


550 is a System.Net.FtpWebRequest connection error code.

Also you have not constructed your FTP correctly check out my FTP class: Its pretty straight forward.

Public Class FTP
        '-------------------------[BroCode]--------------------------
        '----------------------------FTP-----------------------------
        Private _credentials As System.Net.NetworkCredential
        Sub New(ByVal _FTPUser As String, ByVal _FTPPass As String)
            setCredentials(_FTPUser, _FTPPass)
        End Sub
        Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String)
            Dim _FileInfo As New System.IO.FileInfo(_FileName)
            Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
            _FtpWebRequest.Credentials = _credentials
            _FtpWebRequest.KeepAlive = False
            _FtpWebRequest.Timeout = 20000
            _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
            _FtpWebRequest.UseBinary = True
            _FtpWebRequest.ContentLength = _FileInfo.Length
            Dim buffLength As Integer = 2048
            Dim buff(buffLength - 1) As Byte
            Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
            Try
                Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
                Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
                Do While contentLen <> 0
                    _Stream.Write(buff, 0, contentLen)
                    contentLen = _FileStream.Read(buff, 0, buffLength)
                Loop
                _Stream.Close()
                _Stream.Dispose()
                _FileStream.Close()
                _FileStream.Dispose()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Upload Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        Public Sub DownloadFile(ByVal _FileName As String, ByVal _ftpDownloadPath As String)
            Try
                Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpDownloadPath)
                _request.KeepAlive = False
                _request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
                _request.Credentials = _credentials
                Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
                Dim responseStream As System.IO.Stream = _response.GetResponseStream()
                Dim fs As New System.IO.FileStream(_FileName, System.IO.FileMode.Create)
                responseStream.CopyTo(fs)
                responseStream.Close()
                _response.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Download Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
        Public Function GetDirectory(ByVal _ftpPath As String) As List(Of String)
            Dim ret As New List(Of String)
            Try
                Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpPath)
                _request.KeepAlive = False
                _request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
                _request.Credentials = _credentials
                Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
                Dim responseStream As System.IO.Stream = _response.GetResponseStream()
                Dim _reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
                Dim FileData As String = _reader.ReadToEnd
                Dim Lines() As String = FileData.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
                For Each l As String In Lines
                    ret.Add(l)
                Next
                _reader.Close()
                _response.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Directory Fetch Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            Return ret
        End Function

        Private Sub setCredentials(ByVal _FTPUser As String, ByVal _FTPPass As String)
            _credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
        End Sub
    End Class

To initialize:

Dim ftp As New FORM.FTP("username", "password")

ftp.UploadFile("c:\file.jpeg", "ftp://domain/file.jpeg")

ftp.DownloadFile("c:\file.jpeg", "ftp://ftp://domain/file.jpeg")

Dim directory As List(Of String) = ftp.GetDirectory("ftp://ftp.domain.net/")
        ListBox1.Items.Clear()
        For Each item As String In directory
            ListBox1.Items.Add(item)
        Next
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜