我正在尝试将csv文件上传到ftp位置,但由于上述原因,它每次都会失败。
我的代码失败了:Stream requestStream = request.GetRequestStream();
private void button1_Click(object sender, EventArgs e)
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://100.100.100.83");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("guardian", "roar");
request.KeepAlive = true;
request.UsePassive = false;
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("C:/blacklists/blacklist.csv");
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();
}
}
}
答案 0 :(得分:1)
我遇到同样的问题,我用"ftp://100.100.100.83/"
替换"ftp://ftp.xxx.com/"
并且工作正常。
您的网址缺少文件名
ftp://100.100.100.83/blacklist.csv