public static void uploadMethod2()
{
FileInfo toUpload = new FileInfo("0000000002.csv");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.myftp.co.uk/" + toUpload.Name);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("user", "password");
**Stream ftpStream = request.GetRequestStream();**
FileStream file = File.OpenRead("0000000002.csv");
int length = 1024;
byte[] buffer = new byte[length];
int bytesRead = 0;
do
{
bytesRead = file.Read(buffer, 0, length);
ftpStream.Write(buffer, 0, bytesRead);
}
while (bytesRead != 0);
file.Close();
ftpStream.Close();
Console.WriteLine("done");
}
我从 msdn 获得了上面的代码,但我对 webrequest.create 行有点困惑。我把星星放在周围的行返回错误“无法访问或找不到文件”,我希望这是因为目录中还没有一个名为 0000000002.csv 的文件,但是在 msdn 网站上它说要添加我要上传的文件连同 ftp 网址。我尝试不将文件名附加到 url,但这会导致“请求的 URI 对此 FTP 命令无效”错误。我已经在浏览器中尝试了凭据(上面示例中的凭据当然不是实际凭据),并且我明确地让我访问了该目录。有任何想法吗?任何帮助将不胜感激 :)