Problem in Uploading the file to FTP using C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bremanand
    New Member
    • Jul 2006
    • 29

    Problem in Uploading the file to FTP using C#

    Hi folks,
    i am working in C# , i have problem while uploading the file to FTP which has size around 4MB.

    The code which comes after the bold area is not executed.. here am interacting with DB for every succeessful upload. But its working well for the file which has small size around 1MB. Please give me a solution asap... My code is as follows.

    Code:
    string ftpServerIP = rowClient[1].ToString();
    string ftpUserID = rowClient[2].ToString();
    string Password = rowClient[3].ToString();
    
       //////// ---UpLoading...
    FileInfo fileInf = new FileInfo(strfileLocation);
    string uri = ftpServerIP + "/" + fileInf.Name;
    
      // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Timeout = 3600000; // Equvalent to 1 Hour.
    
    
      // This example assumes the FTP site uses anonymous logon.
     request.Credentials = new NetworkCredential(ftpUserID, Password);
    
    // Copy the contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader(fileInf.OpenRead());
        byte[] fileContents =  Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                        sourceStream.Close();
        request.ContentLength = fileContents.Length;
    
        Stream requestStream = request.GetRequestStream();
                        requestStream.Write(fileContents, 0, fileContents.Length);
    
                        requestStream.Close();
    
                        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
                        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
                        
                     [B]   response.Close();[/B]
    
    updateDB(rowClient[0].ToString(), dtime.ToString());
    
    ExecuteNonQuery("update callInfo set status = 'T' where clientId='" + rowClient[0].ToString().Trim() + "' and status ='Q' and date between '" + LastBackUpTime + "' and '" + dtime.ToString() + "'");
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    I'd be surprised if it did no connection open or command in evidence, take a look at this:
    Server-Side Data Access

    Comment

    • Bremanand
      New Member
      • Jul 2006
      • 29

      #3
      Hi there,

      Sorry, I meant that when i am keeping breakpoint in debug mode as well as while running the project, once the line requestStream.C lose(); reached, the control went off... thats it. it never come back to the next line even the upload gets completed... plz let me know the solution for this problem.


      here is the Code again for u

      Code: ( text )

      1. string ftpServerIP = rowClient[1].ToString();
      2. string ftpUserID = rowClient[2].ToString();
      3. string Password = rowClient[3].ToString();
      4.
      5. //////// ---UpLoading...
      6. FileInfo fileInf = new FileInfo(strfil eLocation);
      7. string uri = ftpServerIP + "/" + fileInf.Name;
      8.
      9. // Get the object used to communicate with the server.
      10 FtpWebRequest request = (FtpWebRequest) WebRequest.Crea te(uri);
      11. request.Method = WebRequestMetho ds.Ftp.UploadFi le;
      12. request.Timeout = 3600000; // Equvalent to 1 Hour.
      13.
      14.
      15. // This example assumes the FTP site uses anonymous logon.
      16. request.Credent ials = new NetworkCredenti al(ftpUserID, Password);
      17.
      18. // Copy the contents of the file to the request stream.
      19. StreamReader sourceStream = new StreamReader(fi leInf.OpenRead( ));
      20. byte[] fileContents = Encoding.UTF8.G etBytes(sourceS tream.ReadToEnd ());
      21. sourceStream.Cl ose();
      22. request.Content Length = fileContents.Le ngth;
      23.
      24. Stream requestStream = request.GetRequ estStream();
      25. requestStream.W rite(fileConten ts, 0, fileContents.Le ngth);
      26.
      27. requestStream.C lose();
      28.
      29. FtpWebResponse response = (FtpWebResponse )request.GetRes ponse();
      30.
      31. Console.WriteLi ne("Upload File Complete, status {0}", response.Status Description);
      32.
      33. response.Close( );
      34.

      35. updateDB(rowCli ent[0].ToString(), dtime.ToString( ));
      36.

      37. ExecuteNonQuery ("update callInfo set status = 'T' where clientId='" + rowClient[0].ToString().Tri m() + "' and status ='Q' and date between '" + LastBackUpTime + "' and '" + dtime.ToString( ) + "'");


      Thanks in advance.

      Comment

      Working...