FTP uploading

This is a discussion on FTP uploading within the C# Programming forums, part of the General Programming Boards category; Hi! i managed to upload a file to a remote server with below code: Code: // Get the object used ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362

    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.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362
    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.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362
    i get:
    Code:
    Connected to KasunLiyanage.
    Connection closed by remote host.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    362
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature 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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21