Sending file via FTP in C #, URI error

0

I'm 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 call:

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

Where

path="C: \ data \ file.zip"

URIFTP="ftp: /127.0.0.1"

UserFTP="usr"

PasswordFTP="123"

Always returning error:

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

    
asked by anonymous 11.02.2018 / 21:04

1 answer

0

As you want to upload a file, you must provide the FTPClient with the filename.

var request = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/arquivo.zip");

Source: link

    
11.02.2018 / 23:24