Hola
He utilizado el ejemplo de la documentació de VS para crear una clase que
envia i recibe ficheros mediante FTP.
Funciona correctamente, salvo que cuando finaliza el envio o recepción no
desconecta del servidor. Hay que cerrar la aplicación y volver
ha entrar para repetir un envio o recepción.
Este es un ejemplo del envio de ficheros. Alguien puede decirme que estoy
haciendo mal.
Gracias
Saludos
public void EnviarFichero(string[] fileName)
{
System.IO.Stream ficheroftp = null;
System.IO.FileStream fileStream = null;
System.Net.FtpWebResponse resp = null;
System.Net.FtpWebRequest ftp;
try
{
for (int i = 0; i <= fileName.Length - 1; i++)
{
//ftp =
((System.Net.FtpWebRequest)System.Net.WebRequest.Create("
ftp://localhost/"+
fileName),
// Path.GetFileName( fileName[i]) ));
ftp =
((System.Net.FtpWebRequest)System.Net.WebRequest.Create(Global.Global.IpServidorFtp
+
Path.GetFileName( fileName[i]) ));
//Necesario para autentificarse en el servidor si no
permite conexiones anónimas
System.Net.CredentialCache credCache = new
System.Net.CredentialCache();
credCache.Add(new Uri(Global.Global.IpServidorFtp),
"Basic",
new
System.Net.NetworkCredential(Global.Global.UsuariFtp,
Global.Global.ClauFtp));
ftp.Credentials = credCache;
//******************************
ftp.Method =
System.Net.WebRequestMethods.Ftp.UploadFile;
ftp.Proxy = null;
ficheroftp = ftp.GetRequestStream();
fileStream = System.IO.File.Open(fileName[i],
System.IO.FileMode.Open, System.IO.FileAccess.Read);
long tamany;
tamany = fileStream.Length;
this.PanelInfo.Text = "Enviando...";
this.PanelProgress.Value = 0;
this.PanelProgress.Maximum = ((int)tamaño);
byte[] buffer = new byte[1025];
int bytesLeidos;
while (true)
{
bytesLeidos = fileStream.Read(buffer, 0,
buffer.Length);
OnShowProgres(bytesLlegits);
this.PanelProgress.Value += bytesleidos;
this.Refresh();
if (bytesLlegits == 0)
{
break;
}
ficheroftp.Write(buffer, 0, bytesLlegits);
}
this.PanelInfo.Text = "Fichero enviado";
ficheroftp.Close();
resp = ((System.Net.FtpWebResponse)ftp.GetResponse());
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if( resp != null )
{
resp.Close();
}
if( fileStream != null )
{
fileStream.Close();
}
if( ficheroftp != null )
{
ficheroftp.Close();
}
}
}
Leer las respuestas