Thread: FTP uploading

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    FTP uploading

    Hi!

    i managed to upload a file to a remote server with below code:

    Code:
    // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://kasunl.worlditsme.com/voo.txt");
                request.Method = WebRequestMethods.Ftp.UploadFile;
    
    
                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential("kasunl", "mypass");
    
    
                // Copy the contents of the file to the request stream.
                StreamReader sourceStream = new StreamReader("test.txt");
                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();
    
    
                response.Close();


    My code to upload the file to localhost:

    Code:
    // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost/voo.txt"); 
                request.Method = WebRequestMethods.Ftp.UploadFile;
    
    
                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential("KasunL", "mypass");
    
    
                // Copy the contents of the file to the request stream.
                StreamReader sourceStream = new StreamReader("test.txt");
                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();
    
    
                response.Close();


    i cannot find a solution on web. What i doing wrong


    Thanks
    Last edited by geek@02; 10-01-2012 at 09:54 PM.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > i cannot find a solution on web. What i doing wrong
    Your code doesn't contain any error checking.

    You don't tell us any observations.

    You didn't even tell us what is "wrong". The only thing you said was that you couldn't find "a solution".
    A solution to what?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    Quote Originally Posted by Salem View Post
    > i cannot find a solution on web. What i doing wrong
    Your code doesn't contain any error checking.

    You don't tell us any observations.

    You didn't even tell us what is "wrong". The only thing you said was that you couldn't find "a solution".
    A solution to what?
    i'm so sorry, i'm not sure if i didn't typed it or the text just not appeared. My problem was, i can upload the file to the remote server with code 1, but i cannot upload file to localhost with code-2, because of the errors i'm getting. Now i get the error: "The underlying connection was closed: The server committed a protocol violation.", on the line: Stream requestStream = request.GetRequestStream();. what i'm doing wrong? i have IIS installed. also xamp is installed. might be a conflict?

    Thanks.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What happens if you just open a console, and try
    ftp localhost

    Microsoft Windows XP - Ftp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    i get:
    Code:
    Connected to KasunLiyanage.
    Connection closed by remote host.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I guess you either need to look at the configuration of your local FTP server.

    You seem to be able to establish a connection, because it is the remote (your local FTP server) which initiates the disconnect.

    For some reason, it doesn't seem interested in providing the service to localhost.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ftp uploading error
    By rafay_07 in forum C# Programming
    Replies: 0
    Last Post: 01-10-2012, 09:24 AM
  2. Uploading exe's
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 05-20-2005, 08:42 PM
  3. FTP Uploading
    By Rodrigo in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2003, 01:54 PM