Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello can some one help me to upload a text file to ftp local host i have an error this is my code



string filename = Path.GetFileName("C:\\inetpub\\ftproot\\reg.txt");

               FtpWebRequest request = (FtpWebRequest)WebRequest.Create("127.0.0.1 + ftp/reg.txt");

               request.Method = WebRequestMethods.Ftp.UploadFile;

               request.Credentials = new NetworkCredential("username", "password");

               StreamReader sourceStream = new StreamReader("C:\\inetpub\\ftproot\\reg.txt");

               byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

               request.ContentLength = fileContents.Length;

               Stream requestStream = request.GetRequestStream();

               requestStream.Write(fileContents, 0, fileContents.Length);

               FtpWebResponse response = (FtpWebResponse)request.GetResponse();

               response.Close();

               requestStream.Close();

               sourceStream.Close();
Posted
Comments
Member 11407981 2-Feb-15 12:09pm    
here is the error Error:System.UriFormatException: Invalid URI: The format of the URI could not be determined. at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind) at System.Uri..ctor(String uriString) at System.Net.WebRequest.Create(String requestUriString)

1 solution

As the error message says, the parameter to WebRequest.Create needs to be a valid URI. The value you've passed in ("127.0.0.1 + ftp/reg.txt") is not a valid URI.

The URI should look something like:
"ftp://127.0.0.1/reg.txt"


However, this code doesn't make any sense. You're taking a file from the server, and trying to upload it via FTP to the same location on the same server!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Feb-15 12:25pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900