Link to home
Start Free TrialLog in
Avatar of mpdillon
mpdillon

asked on

VB.net FTP error - The remote server returned an error: File Unavailable...

In the following code I am trying to upload a file to a server. The code runs up until the line I have highlighted with ****>>>>. The line begins Dim Stream...
The error states:
The remote server returned an error:(550) File Unavailable(e.g. File not found, no access.)

I do not believe the error is due to access. Using the FTP client Core-Lite, I am able to upload a file to the directory.

What do I need to change to upload a file.

Thanks,
pat


Public Sub FTPUploadFile(ByVal localfile As String, ByVal remotefile As String)
        '- Create request
        Dim request As FtpWebRequest = WebRequest.Create("ftp://" & PublicVariables.FTPClientIDStringP & "/" & remotefile)
        request.Method = WebRequestMethods.Ftp.UploadFile
        request.Credentials = New NetworkCredential(PublicVariables.FTPUserNameStringP, PublicVariables.FTPPasswordStringP)
        request.UsePassive = True
        request.UseBinary = True
        request.KeepAlive = False

        '- Upload file
        Dim fileContents As Byte() = System.IO.File.ReadAllBytes(localfile)
***>>> Error Here   Dim stream As System.IO.Stream = request.GetRequestStream()  <<<******
        stream.Write(fileContents, 0, fileContents.Length)
        stream.Close()
        stream.Dispose()

        '- Handle response here if you'd like ...
        'Dim response As FtpWebResponse = request.GetResponse()
        'response.Close()
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mpdillon
mpdillon

ASKER

kaufmed,
Doesn't sound silly at all. More often than not I usually mess something up that is very simple and just taken for granted. In this case however, the user name and password were correct.
I found a solution by using slightly differnt code. The code below is working for me.
Thanks,
pat
Dim reqObj As FtpWebRequest = WebRequest.Create("ftp://" & PublicVariables.FTPClientIDStringP & "/" & remotefile)


        'Call A FileUpload Method of FTP Request Object
        reqObj.Method = WebRequestMethods.Ftp.UploadFile

        'If you want to access Resourse Protected You need to give User Name      and PWD
        reqObj.Credentials = New NetworkCredential(PublicVariables.FTPUserNameStringP, PublicVariables.FTPPasswordStringP)


        'FileStream object read file from Local Drive
        Dim streamObj As FileStream = File.OpenRead(localfile)


        'Store File in Buffer
        Dim buffer(streamObj.Length) As Byte


        'Read File from Buffer
        streamObj.Read(buffer, 0, buffer.Length)


        'Close FileStream Object Set its Value to nothing
        streamObj.Close()
        streamObj = Nothing


        'Upload File to ftp://localHost/ set its object to nothing
        reqObj.GetRequestStream().Write(buffer, 0, buffer.Length)
        reqObj = Nothing

Open in new window

FTP upload is working with new code
Glad you got it working  :)