.NET Framework Developer Center > .NET Development Forums > .NET Framework Networking and Communication > Uploading a file -- "The requested URI is invalid for this FTP command"
Ask a questionAsk a question
 

AnswerUploading a file -- "The requested URI is invalid for this FTP command"

  • Monday, June 05, 2006 3:12 PMb182f117 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

  • Tuesday, June 06, 2006 2:17 PMb182f117 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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

  • Tuesday, June 06, 2006 2:17 PMb182f117 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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.
  • Friday, July 14, 2006 2:05 PMKaoticfen Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks so much for this post. I had the exact same problem with my application.Thanks again
  • Tuesday, October 10, 2006 8:57 PMKaa Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks so much for posting your resolution.  I was having precisely the same problem, and that fixed it.
  • Tuesday, December 12, 2006 10:22 PMrobertje Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    FtpWebRequest uploadRequest =(FtpWebRequest)WebRequest.Create(website.com + @"/" + localfile.html");

    Is this how it should look?

  • Thursday, June 07, 2007 11:19 PMkajester Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Wednesday, January 16, 2008 5:47 AMkaushalparik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanx for sharing the resolution for this problem... even i had the same problem... and it worked...!! thanx again..
  • Friday, May 23, 2008 8:57 PMFlea_ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 Snippet

    FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(uploadUrl + "/" + 
                              Path.GetFileName(fileName));


  • Tuesday, July 29, 2008 4:01 AMviki2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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..
  • Tuesday, April 28, 2009 10:00 AMTryst Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Helped me too!

    thanks!
    Tryst
  • Wednesday, August 26, 2009 4:04 AMnirvigna Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    helped me too as well...
  • Thursday, November 05, 2009 7:15 AMPulin Zala Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    it helped me too.

    thanks
  • Thursday, November 05, 2009 7:49 PMJupudi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you very much. It just worked!