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