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
– Luis Fernando Consulo Martins
@Luisfernandoconsulomartins Putting 2 bars instead of 1 solves the problem?
– Maniero
No, I tried with two and one bar in both the FTP URI and the directory, same error
– Luis Fernando Consulo Martins
Tried to declare paths as strings with no escape character? Like this:
caminho = @"C:\dados\arquivo.zip"; e URIFTP = @"ftp://127.0.0.1";
– Diego Rafael Souza
$Exception {"Invalid URI: Invalid URI scheme." } System.Uriformatexception @Diegorafaelsouza
– Luis Fernando Consulo Martins
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– Diego Rafael Souza
You may want to consider this: https://github.com/dotnet/platform-compatblob/master/docs/DE0003.md
– user178974