C#ftp文件上传不起作用

时间:2014-04-30 06:29:26

标签: c# file-upload ftp

我使用的是从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();

怎么办?

1 个答案:

答案 0 :(得分:0)

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

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

将您的文件放入磁盘D: