代码是在MSDN上的,稍做改变
public static void Do()
        {
            // uploadUrl="ftp://(IP)/testftp/test.txt"
            string uploadUrl = Config.GetFtpServerUri();
            // fileName ="c:\test.txt"
            string fileName = Config.GetCsvFilePath();
            // Create a Uri instance with the specified URI string.
            // If the URI is not correctly formed, the Uri constructor
            // will throw an exception.
            ManualResetEvent waitObject;            Uri target = new Uri(uploadUrl);
            FtpState state = new FtpState();
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            
            // The request is anonymous by default; the credential does not have to be specified. 
            // The example specifies the credential only to
            // control how actions are logged on the server.
            string userName = Config.GetFtpUser();
            string userPwd = Config.GetFtpUserPassword();            request.Credentials = new NetworkCredential(userName, userPwd);
            // request.
            // set 
            // request.EnableSsl = true;            // Store the request in the object that we pass into the
            // asynchronous operations.
            state.Request = request;
            state.FileName = fileName;            // Get the event to wait on.
            waitObject = state.OperationComplete;            // Asynchronously get the stream for the file contents.
            request.BeginGetRequestStream(
                new AsyncCallback(EndGetStreamCallback),
                state
            );            // Block the current thread until all operations are complete.
            waitObject.WaitOne();            // The operations either completed or threw an exception.
            if (state.OperationException != null)
            {
                throw state.OperationException;
            }
            else
            {
                Console.WriteLine("The operation completed - {0}", state.StatusDescription);
            }
        }        private static void EndGetStreamCallback(IAsyncResult ar)
        {
            FtpState state = (FtpState)ar.AsyncState;            Stream requestStream = null;
            // End the asynchronous call to get the request stream.
            try
            {
                requestStream = state.Request.EndGetRequestStream(ar);
                // Copy the file contents to the request stream.
                const int bufferLength = 2048;
                byte[] buffer = new byte[bufferLength];
                int count = 0;
                int readBytes = 0;
                FileStream stream = File.OpenRead(state.FileName);
                do
                {
                    readBytes = stream.Read(buffer, 0, bufferLength);
                    requestStream.Write(buffer, 0, readBytes);
                    count += readBytes;
                }
                while (readBytes != 0);
                Console.WriteLine("Writing {0} bytes to the stream.", count);
                // IMPORTANT: Close the request stream before sending the request.
                requestStream.Close();
                // Asynchronously get the response to the upload request.
                state.Request.BeginGetResponse(
                    new AsyncCallback(EndGetResponseCallback),
                    state
                );
            }
            // Return exceptions to the main application thread.
            catch (Exception e)
            {
                Console.WriteLine("Could not get the request stream.");
                state.OperationException = e;
                state.OperationComplete.Set();
                return;
            }        }在执行到requestStream = state.Request.EndGetRequestStream(ar);就总是报550错误,
ftp是在linux上用vsftpd搭建的,
用useradd -g ftp testftp创建的用户,这是为什么,怎么解决,不胜感激!

解决方案 »

  1.   

    我在用smartFtp工具就能上传,不知道这个为什么报这个错误,
    不知道是不是"ftp://(IP)/testftp/test.txt"这个有问题,
      

  2.   

    有没有用socket实现的?介绍一下经验
    实在不行,我打算用socket来实现了
      

  3.   

    用socket调用ftp命令应该是可以的吧,
      

  4.   

    target, 上传的路径在ftp上真实存在吗?
      

  5.   

    报的是路径错误, 把target打印出来看看
      

  6.   

    你可以先请求FTP上这个文件夹中的一个文件,根据获取的流大小,判断文件是否存在,是则上传,否则就创建文件夹..下面是MSDN上的代码   ..参考下给你:   
        
      ===========================================   
        
      static   void   Upload(string   fileName,   string   uploadUrl)   
                      {   
                              Stream   requestStream   =   null;   
                              FileStream   fileStream   =   null;   
                              FtpWebResponse   uploadResponse   =   null;   
                              try   
                              {   
                                      FtpWebRequest   uploadRequest   =   
                                              (FtpWebRequest)WebRequest.Create(uploadUrl);   
                                      uploadRequest.Method   =   WebRequestMethods.Ftp.UploadFile;   
        
                                      //   UploadFile   is   not   supported   through   an   Http   proxy   
                                      //   so   we   disable   the   proxy   for   this   request.   
                                      uploadRequest.Proxy   =   null;   
        
                                      requestStream   =   uploadRequest.GetRequestStream();   
                                      fileStream   =   File.Open(fileName,   FileMode.Open);   
        
                                      byte[]   buffer   =   new   byte[1024];   
                                      int   bytesRead;   
                                      while   (true)   
                                      {   
                                              bytesRead   =   fileStream.Read(buffer,   0,   buffer.Length);   
                                              if   (bytesRead   ==   0)   
                                                      break;   
                                              requestStream.Write(buffer,   0,   bytesRead);   
                                      }   
        
                                      //   The   request   stream   must   be   closed   before   getting     
                                      //   the   response.   
                                      requestStream.Close();   
        
                                      uploadResponse   =   
                                              (FtpWebResponse)uploadRequest.GetResponse();   
                                      Console.WriteLine("Upload   complete.");   
                              }   
                              catch   (UriFormatException   ex)   
                              {   
                                      Console.WriteLine(ex.Message);   
                              }   
                              catch   (IOException   ex)   
                              {   
                                      Console.WriteLine(ex.Message);   
                              }   
                              catch   (WebException   ex)   
                              {   
                                      Console.WriteLine(ex.Message);   
                              }   
                              finally   
                              {   
                                      if   (uploadResponse   !=   null)   
                                              uploadResponse.Close();   
                                      if   (fileStream   !=   null)   
                                              fileStream.Close();   
                                      if   (requestStream   !=   null)   
                                              requestStream.Close();   
                              }   
                      }   
      

  7.   

    "ftp://(IP)/testftp/test.txt"
    应该是
    "ftp://(IP)/test.txt"