上传文件到ftp c#

时间:2013-06-12 09:33:59

标签: c# web ftp

我试图将文件上传到ftp服务器并且我不断收到“webexception无法连接到未登录的远程服务器530”

剂量任何人都知道如何解决这个问题? 你试图禁用我的防火墙,但它没有效果

 /* Create an FTP Request */
                FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(ftpurl + "/" + remoteFile);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(ftpuser, ftppassword,ftpurl);
                /* When in doubt, use these options */
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                /* Establish Return Communication with the FTP Server */
              Stream  ftpStream = ftpRequest.GetRequestStream();
                /* Open a File Stream to Read the File for Upload */
                FileStream localFileStream = new FileStream(localFile, FileMode.Create);
                /* Buffer for the Downloaded Data */
                byte[] byteBuffer = new byte[bufferSize];
                int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */

                {
                    while (bytesSent != 0)
                    {
                        ftpStream.Write(byteBuffer, 0, bytesSent);
                        bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                    }
                }

                /* Resource Cleanup */
                localFileStream.Close();
                ftpStream.Close();
                ftpRequest = null;

1 个答案:

答案 0 :(得分:0)

你的NetworkCredential似乎错了。第三个参数设置域(参见:NetworkCredential Constructor)。尝试将其更改为:

ftpRequest.Credentials = new NetworkCredential(ftpuser, ftppassword);