【问题标题】:Uploading to FTP server results to zero byte size in c#在 c# 中上传到 FTP 服务器导致字节大小为零
【发布时间】: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


【解决方案1】:

现在解决了,我刚刚将 FileMode.Create 替换为 FileMode.Open

【讨论】:

    猜你喜欢
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    相关资源
    最近更新 更多