Uploading a file -- "The requested URI is invalid for this FTP command"
I looked at another post that had the same problem, but it didn't really help me out. I have the following code to upload a file with (C#):
public void Upload(string fileName, string uploadUrl, string user, string pswd)
{
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
try
{
FtpWebRequest uploadRequest =
(FtpWebRequest)WebRequest.Create(uploadUrl);
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
//Since the FTP you are downloading to is secure, send
//in user name and password to be able upload the file
ICredentials credentials = new NetworkCredential(user, pswd);
uploadRequest.Credentials = credentials;
//UploadFile is not supported through an Http proxy
//so we disable the proxy for this request.
uploadRequest.Proxy = null;
//uploadRequest.UsePassive = false; <--found from another forum and did not make a difference
requestStream = uploadRequest.GetRequestStream();
fileStream = File.Open(fileName, FileMode.Open);
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = fileStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
requestStream.Write(buffer, 0, bytesRead);
}
//The request stream must be closed before getting
//the response.
requestStream.Close();
uploadResponse =
(FtpWebResponse)uploadRequest.GetResponse();
}
catch (UriFormatException ex)
{
ErrorsHappened.Items.Add("Error: " + ex.Message);
}
catch (IOException ex)
{
ErrorsHappened.Items.Add("Error: " + ex.Message);
}
catch (WebException ex)
{
ErrorsHappened.Items.Add("Error: " + ex.Message);
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
}The problem is the code will get to the line "requestStream = uploadRequest.GetRequestStream();" and fail, throwing the exception "The requested URI is invalid for this FTP command." under the catch for WebException. I have used the filename to be the exact spot the file is I want to download. I know the username and password are correct. I have tried the UploadURL to be just the place I want to upload and I have also tried it to be the place I want upload to plus the name of the file. Neither have worked. (ex: file- ftp://localhost/test1.txt uploadLocation- ftp://localhost OR ftp://localhost/test1.txt -- I do realize I am taking a file and uploading it to the same place, but I have also tried to upload a file that was sitting in MyDocuments but to no avail either)
Answers
- FtpWebRequest uploadRequest = (FtpWebRequest) Webrequest.Create(uploadUrl + @"/" + fileName) -- where uploadUrl is the place to upload to and fileName is just the name of the file. It turns out that my filename ended up being the location and name of the file and therefore it did not work.

All Replies
- FtpWebRequest uploadRequest = (FtpWebRequest) Webrequest.Create(uploadUrl + @"/" + fileName) -- where uploadUrl is the place to upload to and fileName is just the name of the file. It turns out that my filename ended up being the location and name of the file and therefore it did not work.

- Thanks so much for this post. I had the exact same problem with my application.Thanks again
- Thanks so much for posting your resolution. I was having precisely the same problem, and that fixed it.
FtpWebRequest
uploadRequest =(FtpWebRequest)WebRequest.Create(website.com + @"/" + localfile.html");Is this how it should look?
- This was about the most helpful post on this topic to date! Thank you so much for the code sample, as it really did get me through the problems I was experiencing.
Thanx for sharing the resolution for this problem... even i had the same problem... and it worked...!! thanx again..- I was having problems getting the above example to work. It was still having trouble recognizing the file path, however this code below did the trick. Just one more option for anyone else having this issue:
Code SnippetFtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(uploadUrl + "/" +
Path.GetFileName(fileName)); - Thanks for Solution. I have one problem here. I have to change the working directory in ftp server, so i can upload the file.
How can we change the directory path.
viki.. - Helped me too!
thanks!
Tryst - helped me too as well...
- it helped me too.thanks
- Thank you very much. It just worked!


