6 Replies - 2817 Views - Last Post: 02 October 2012 - 06:31 AM Rate Topic: -----

#1 KasunL  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 11-April 11

FTP file upload problem

Posted 01 October 2012 - 12:50 AM

Hi!

i'm trying to upload a file to localhost using FTP with below code. i get error: "Unable to connect to the remote server" on
Stream requestStream = request.GetRequestStream();
What i'm doing wrong? i'm using xamp as the local server.

// Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost/");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("root", "");

            // 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();



Thanks.

This post has been edited by KasunL: 01 October 2012 - 12:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: FTP file upload problem

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: FTP file upload problem

Posted 01 October 2012 - 02:26 AM

It would be helpful if you tell us what error you get.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: FTP file upload problem

Posted 01 October 2012 - 03:13 AM

You don't have an FTP server running on localhost, or the firewall is blocking inbound requests to the localhost's FTP port?
Was This Post Helpful? 0
  • +
  • -

#4 KasunL  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 11-April 11

Re: FTP file upload problem

Posted 01 October 2012 - 04:55 AM

i installed IIS and set up an ftp server, and tried to upload the file to the server folder. But now i get the error: "The requested URI is invalid for this FTP command.", at the same line as previous. (My server is running i can access it with the browser.)

Code:

// Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://10.224.213.152/"); // Tried ftp://localhost/ too, without success
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("Kasun Liyanage", "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();


This post has been edited by KasunL: 01 October 2012 - 04:57 AM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: FTP file upload problem

Posted 01 October 2012 - 05:04 AM

I believe you will need to provide a file name to save to on the server in your URL.

See this, which is the first link of a Google search of this error. Please learn to use Google first to fix your problems. If you get an error, guess what...you're probably not the first person on the planet to get that error.
Was This Post Helpful? 0
  • +
  • -

#6 KasunL  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 11-April 11

Re: FTP file upload problem

Posted 01 October 2012 - 07:37 AM

Ok thank i managed to upload a file into a remote web server.

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();



But i can not figure out or find a answer on web to why uploading to localhost cause errors. Latest error: The underlying connection was closed: The server committed a protocol violation.

// Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://localhost/voo.txt"); // Tried ftp://localhost/ too, without success
            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();


Was This Post Helpful? 0
  • +
  • -

#7 KasunL  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 11-April 11

Re: FTP file upload problem

Posted 02 October 2012 - 06:31 AM

Ok i did figure out how to do it and made a quick blog post for future reference, and for any ftp newbie that might be in the same trouble as was me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1