I need to upload file via ftp to host. The /home2/travele2
path created on the root of host
我可以通过 FileZilla 程序将文件上传到主机,但是当我尝试通过网站上传文件时,出现以下错误:
远程服务器返回错误:(550) 文件不可用(例如,未找到文件、无法访问)。
问题是什么?
// 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 必须包含目标文件名:
string url = "ftp://ftp.example.com/home2/travele2/Site.pdf";
FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(url);
不然怎么会FtpWebRequest
知道,该用什么名字?
一旦解决了这个问题,您就会发现上传损坏了文件,因为您将文件视为 UTF-8 编码文本。什么是废话,因为 PDF 是二进制文件。
正确的代码请参见:
在 C#/.NET 中向 FTP 服务器上传和下载文件 https://stackoverflow.com/q/44606028/850848
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)