gpt4 book ai didi

c# - 远程服务器返回错误 : (550) on upload file to FTP using FtpWebRequest

转载 作者:行者123 更新时间:2023-11-30 16:42:32 24 4
gpt4 key购买 nike

我需要通过 ftp 上传文件到主机。在主机根目录下创建的/home2/travele2路径 enter image description here

我可以通过 FileZilla 程序将文件上传到主机,但是当我尝试通过网站上传文件时,出现以下错误:

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();

enter image description here

最佳答案

URL 必须包含目标文件名:

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

FtpWebRequest 还怎么知道要使用什么名称?


一旦您解决了这个问题,您会发现上传会损坏文件,因为您将文件视为 UTF-8 编码的文本。废话,因为PDF是二进制文件。

有关正确的代码,请参阅:
Upload and download a file to/from FTP server in C#/.NET

关于c# - 远程服务器返回错误 : (550) on upload file to FTP using FtpWebRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46580751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com