1

I am trying to upload a file on FTP folder, but getting the following error.

The remote server returned an error: (550) File unavailable (e.g., file not found, no access)

I am using the following sample to test this:

    // Get the object used to communicate with the server.
    string path = HttpUtility.UrlEncode("ftp://host:port//01-03-2017/John, Doe S. M.D/file.wav");
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
    request.Method = WebRequestMethods.Ftp.UploadFile;

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

    // Copy the contents of the file to the request stream.
    StreamReader sourceStream = new StreamReader(@"localpath\example.wav");
    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();
  • I am able to upload files on the parent folder 01-03-2017 but not in the target folder ROLLINS, SETH S. M.D which clearly has special characters in it.
  • I am able to upload files using FileZilla
  • I have tried to HttpUtility.UrlEncode but that did n't help

Thanks for your time and help.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ankit
  • 2,448
  • 3
  • 20
  • 33

3 Answers3

2

Use something like this:

string path = HttpUtility.UrlEncode("ftp://96.31.95.118:2121//01-03-2017//ROLLINS, SETH S. M.D//30542_3117.wav");

or You can form a Uri using the following code and pass it webrequest.

var path = new Uri("ftp://96.31.95.118:2121//01-03-2017//ROLLINS, SETH S. M.D//30542_3117.wav");
Praburaj
  • 613
  • 8
  • 21
  • This will get you `ftp%3a%2f%2f96.31.95.118%3a2121%2f%2f01-03-2017%2f%2fROLLINS%2c+SETH+S.+M.D%2f%2f30542_3117.wav`. That does not look like a correct way (even if it works by chance). – Martin Prikryl Jan 03 '17 at 07:54
  • @MartinPrikryl, Yes, this solution is incorrect. My comment is misplaced and should have been after Prabhu's comment on the question. – Ankit Jan 03 '17 at 08:02
2

You need to encode the spaces (and maybe commas) in the URL path, like:

string path =
    "ftp://host:port/01-03-2017/" +
    HttpUtility.UrlEncode("John, Doe S. M.D") + "/file.wav";

Effectively, you get:

ftp://host:port/01-03-2017/John%2c+Doe+S.+M.D/file.wav
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
2

The code works on a C# console application but did not work in Web Api Action. I could not manage to find the reason.

So I have used a free library for the same.

Posting the sample code from one of the examples available here:

So i have used FluentFtp libary available through Nuget.

using System;
using System.IO;
using System.Net;
using FluentFTP;

namespace Examples {
    public class OpenWriteExample {
        public static void OpenWrite() {
            using (FtpClient conn = new FtpClient()) {
                conn.Host = "localhost";
                conn.Credentials = new NetworkCredential("ftptest", "ftptest");

                using (Stream ostream = conn.OpenWrite("01-03-2017/John, Doe S. M.D/file.wav")) {
                    try {
                        // istream.Position is incremented accordingly to the writes you perform
                    }
                    finally {
                        ostream.Close();
                    }
                }
            }
        }
    }
}

Again, if the file is a binary file, StreamReader should not be used as explained here.

Community
  • 1
  • 1
Ankit
  • 2,448
  • 3
  • 20
  • 33