Reputation: 2354
I need to upload file via ftp to host. The /home2/travele2
path created on the root of host
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();
Upvotes: 3
Views: 5402
Reputation: 202272
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
Upvotes: 4
Reputation: 106
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
Upvotes: -2