远程服务器返回错误:(550)使用FtpWebRequest将文件上传到FTP [英] The remote server returned an error: (550) on upload file to FTP using FtpWebRequest
问题描述
我需要通过ftp上传文件到主机.在主机根目录上创建的/home2/travele2
路径
I need to upload file via ftp to host. The /home2/travele2
path created on the root of host
我可以通过FileZilla程序将文件上传到主机,但是当我尝试通过网站上传文件时,出现此错误:
I can upload file via FileZilla program to the host, but when I try to upload file via website, it gets me this error:
远程服务器返回错误:(550)文件不可用(例如,文件未找到,无法访问).
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
出什么问题了?
// 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();
推荐答案
URL必须包含目标文件名:
The URL has to include a target file name:
FtpWebRequest ftpWebRequest =
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/home2/travele2/Site.pdf");
FtpWebRequest
还怎么知道,要使用什么名称?
How else would the FtpWebRequest
know, what name to use?
解决该问题后,您会发现上载损坏了该文件,因为您将该文件视为UTF-8编码的文本.什么是废话,因为PDF是二进制文件.
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.
有关正确的代码,请参阅:
在C#/.NET的FTP服务器上向/从FTP服务器上下载二进制文件
For a correct code, see:
Upload and download a binary file to/from FTP server in C#/.NET
这篇关于远程服务器返回错误:(550)使用FtpWebRequest将文件上传到FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!