我正在运行以下内容:
try
{
var ftpWebRequest = (FtpWebRequest)WebRequest.Create("ftp://webftp.ftp.com");
ftpWebRequest.Credentials = new NetworkCredential("user", "password");
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
using (var inputStream = File.OpenRead(@data))
using (var outputStream = ftpWebRequest.GetRequestStream())
{
var buffer = new byte[1024 * 1024];
int totalReadBytesCount = 0;
int readBytesCount;
while ((readBytesCount = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, readBytesCount);
totalReadBytesCount += readBytesCount;
var progress = totalReadBytesCount * 100.0 / inputStream.Length;
uploader.ReportProgress((int)progress);
}
}
}
catch (Exception es)
{
MessageBox.Show(es.ToString());
}
@data 是文件的完整本地路径。
我每次收到的错误如下:
System.Net.WebException:请求的 URI 对于此 FTP 命令无效。在 System.Net.FtpWebRequest.GetRequestStream()
我已经通过 cmd 尝试了 ftp 连接,它工作得非常好。