简体   繁体   中英

C# ftp file upload not working

I am using this code that I got from 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();

But I am getting this error:

The remote server returned an error: (553) File name not allowed.

On this line Stream requestStream = request.GetRequestStream();

What to do?

You have to show full local path in StreamReader. Try this

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

Put your file in disk D:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM