本地网络上的FTP文件上传C#

时间:2016-04-07 13:14:50

标签: c# networking upload ftp

我在FTP服务器上传文件时遇到问题。 当我在我的计算机(127.0.0.1)上运行FileZilla服务器时,图像文件已成功上载。但我在同一网络中的另一台计算机上运行FileZilla服务器。(10.0.1.25)。我可以在这台计算机上创建目录,但我无法上传图像文件,尽管我的用户可以完全控制这台计算机。

 public bool Upload(Stream srcStream, string dstFilePath)
    {
         Create FtpWebRequest object from the Uri provided
        FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(serverUri + dstFilePath));
        reqFTP.Credentials = credential;
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
        reqFTP.UseBinary = true;
        reqFTP.Proxy = null;
        reqFTP.UsePassive = false;
        reqFTP.KeepAlive = false;
        reqFTP.ContentLength = srcStream.Length;
        byte[] buff = new byte[UPLOAD_DOWNLOAD_BUFFER_SIZE];
        int contentLen;

        // Stream to which the file to be upload is written

            using (Stream dstStream = reqFTP.GetRequestStream())
            {
                // Read from the file stream 2kb at a time
                contentLen = srcStream.Read(buff, 0, UPLOAD_DOWNLOAD_BUFFER_SIZE);

                // Till Stream content ends
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the FTP Upload Stream
                    dstStream.Write(buff, 0, contentLen);
                    contentLen = srcStream.Read(buff, 0, UPLOAD_DOWNLOAD_BUFFER_SIZE);
                }

                dstStream.Close();
            }
        }

        // Get the response to the upload request.
        bool ret;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        // ret = (response.StatusCode == FtpStatusCode.ClosingData);    // 
        response.Close();

        ret = (GetFileSize(dstFilePath) == srcStream.Length);

        return ret;
    }

当我将第9行更改为reqFTP.UsePassive = true时,服务器返回(227进入被动模式(h1,h2,h3,h4,p1,p2))。现在它返回(500)语法错误,命令无法识别。会有什么问题? 感谢

1 个答案:

答案 0 :(得分:0)

我可以在防病毒运行时上传图片Filezilla。但是,当我通过使用我的代码块尝试它。它返回端口错误消息。防病毒停止后,一切运行良好。