Sending file via FTP in C#, URI error

Asked

Viewed 843 times

1

I am trying to send a file via ftp using the class below:

public static void EnviarArquivoFTP(string arquivo, string url, string usuario, string senha)
{
    try
    {
        FileInfo arquivoInfo = new FileInfo(arquivo);

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(url));

        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(usuario, senha);
        request.UseBinary = true;
        request.ContentLength = arquivoInfo.Length;

        using (FileStream fs = arquivoInfo.OpenRead())
        {
            byte[] buffer = new byte[2048];
            int bytesSent = 0;
            int bytes = 0;

            using (Stream stream = request.GetRequestStream())
            {
                while (bytesSent < arquivoInfo.Length)
                {
                    bytes = fs.Read(buffer, 0, buffer.Length);
                    stream.Write(buffer, 0, bytes);
                    bytesSent += bytes;
                }
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Using the so-called:

ftp.EnviarArquivoFTP(caminho, URIFTP, UsuarioFTP, SenhaFTP);

Where

path = "C: .zip file data"

URIFTP = "ftp:/127.0.0.1"

User ftp = "usr"

Password = "123"

Always returning the error:

$Exception {"The requested URI is invalid for the FTP command."} System.Net.Webexception

  • Yes, running a location, it gives URI error, not connection, Filezilla Server in case

  • @Luisfernandoconsulomartins Putting 2 bars instead of 1 solves the problem?

  • No, I tried with two and one bar in both the FTP URI and the directory, same error

  • Tried to declare paths as strings with no escape character? Like this: caminho = @"C:\dados\arquivo.zip"; e URIFTP = @"ftp://127.0.0.1";

  • $Exception {"Invalid URI: Invalid URI scheme." } System.Uriformatexception @Diegorafaelsouza

  • The scheme is just the first part. I don’t know how your method is EnviarArquivoFtp but if waiting string it can stwe doing the treatment in there. Sends only the path, to see if the schema and the door it is treating internally : URIFTP = @"127.0.0.1”;. If it doesn’t, we’ll need to see what’s inside that method

  • You may want to consider this: https://github.com/dotnet/platform-compatblob/master/docs/DE0003.md

Show 2 more comments

1 answer

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.