C#ftp文件上传不起作用

本文关键字:不起作用 文件 C#ftp | 更新日期: 2023-09-27 18:00:48

我使用的是从MSDN获得的以下代码:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://a.b.com/home/grad/G140315METST-HT001.jpg");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("user", "pass");
StreamReader sourceStream = new StreamReader("G140315METST-HT001.jpg");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();

但我得到了这个错误:

远程服务器返回错误:(553)不允许使用文件名

在这条线上Stream requestStream = request.GetRequestStream();

该怎么办?

C#ftp文件上传不起作用

您必须在StreamReader中显示完整的本地路径。试试这个

StreamReader sourceStream = new StreamReader("D:''G140315METST-HT001.jpg");

将文件放入磁盘D: