"Requested URI is invalid" during upload with FtpWebRequest

23,311

Solution 1

If you want to upload something, you'll have to provide the FTPClient with a filename.

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45/myNewFile.dat");

Solution 2

I suggest you use WebClient which is a higher level abstraction and works with HTTP and FTP and has much simpler API and performance-wise pretty the same (uses the same API).

Here is upload data.

Share:
23,311
redrom
Author by

redrom

Updated on May 13, 2020

Comments

  • redrom
    redrom about 4 years

    I trying upload file to a directory on a FTP server. I used this method with FtpWebRequest. I would like to upload one file to a home directory for this user, but I always get the following error message:

    The requested URI is invalid for this FTP command.

    What can be problem? I tried use passive mode off, but it still the same.

    static void FtpUpload()
    {
    
    
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://12.22.44.45");
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.UsePassive = false;
    
        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential("pokus", "password");
    
        // Copy the contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader(path);
        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();
    
        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
    
        response.Close();
    
    }