上传到 FTP 服务器结果在 c# 中为零字节大小
Uploading to FTP server results to zero byte size in c#
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
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