The remote server returned an error: (550) on upload file to FTP using FtpWebRequest
Asked Answered
M

2

3

I need to upload file via ftp to host. The /home2/travele2 path created on the root of host enter image description here

I can upload file via FileZilla program to the host, but when I try to upload file via website, it gets me this error:

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

What is the problem?

// Get the object used to communicate with the server.  
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://00.00.00.00/home2/travele2");
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.  
ftpWebRequest.Credentials = new NetworkCredential("aaaaaaa", "0000000");

// Copy the contents of the file to the request stream.  
StreamReader sourceStream = new StreamReader(Server.MapPath("/Content/Site.pdf"));
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
ftpWebRequest.ContentLength = fileContents.Length;

Stream requestStream = ftpWebRequest.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();

FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse();

Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

response.Close();

enter image description here

Manger answered 5/10, 2017 at 8:6 Comment(0)
H
4

The URL has to include a target file name:

string url = "ftp://ftp.example.com/home2/travele2/Site.pdf";
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(url);

How else would the FtpWebRequest know, what name to use?


And once you have that solved, you will find out that the upload corrupts the file, as you treat the file as UTF-8-encoded text. What is nonsense, as a PDF is a binary file.

For a correct code, see:
Upload and download a file to/from FTP server in C#/.NET

Horowitz answered 5/10, 2017 at 8:27 Comment(0)
A
-2

The way you load your sourceStream looks kind of fishy.. I would recommend to download a nuget library ftp client, it will make your life a lot easier. FluentFTP is a good choice with good documentation and examples.

Check it out: https://github.com/robinrodricks/FluentFTP

Antimatter answered 5/10, 2017 at 8:14 Comment(2)
When recommending a software, you should explicitly state if you are affiliated to the project or not.Neel
First of all it's an open source library... And no I'm not related in any way to this project.Antimatter

© 2022 - 2024 — McMap. All rights reserved.