我尝试将文件上传到FTP服务器上的目录。我用这个方法FtpWebRequest
。我想将一个文件上传到该用户的主目录,但我总是收到以下错误消息:
请求的URI对此FTP命令无效。
有什么问题?我试过关闭被动模式,但它仍然是一样的。
static void FtpUpload()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = false;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("pokus", "password");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(path);
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();
}
发布于 2019-01-04 13:28:22
我建议你使用WebClient,这是一个更高级别的抽象,并与HTTP和FTP一起使用,并且具有更简单的API和性能方面相同(使用相同的API)。
发布于 2019-01-04 15:06:30
如果要上传某些内容,则必须为FTPClient提供文件名。
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45/myNewFile.dat");
https://stackoverflow.com/questions/-100006355
复制相似问题