【发布时间】:2017-04-29 08:11:34
【问题描述】:
public void upload(string remoteFile, string localFile)
{
MessageBox.Show(remoteFile);
MessageBox.Show(localFile);
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpStream = ftpRequest.GetRequestStream();
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesSent != 0)
{
ftpStream.Write(byteBuffer,0, bytesSent);
bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
localFileStream.Close();
ftpStream.Close();
ftpRequest = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return;
}
请检查上面我在我的代码中使用的代码。
【问题讨论】:
-
现在解决了,我刚刚将 FileMode.Create 替换为 FileMode.Open
-
拜托,您能否将您的解决方案作为答案并接受它,以免贡献者阅读您的问题。
标签: c# upload ftp filestream