我尝试将文件上传到 FTP 服务器上的目录.我将此方法与 FtpWebRequest
一起使用.我想为该用户上传一个文件到主目录,但我总是收到以下错误消息:
I trying upload file to a directory on a FTP server. I used this method with FtpWebRequest
.
I would like to upload one file to a home directory for this user, but I always get the following error message:
请求的 URI 对于此 FTP 命令无效.
The requested URI is invalid for this FTP command.
可能有什么问题?我试过关闭被动模式,但还是一样.
What can be problem? I tried use passive mode off, but it still the same.
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();
}
如果你想上传一些东西,你会 必须为 FTPClient 提供文件名.
If you want to upload something, you'll have to provide the FTPClient with a filename.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45/myNewFile.dat");
这篇关于“请求的 URI 无效"在使用 FtpWebRequest 上传期间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!