/* Upload File */ public void Upload(string localFile, string remoteFile) { try { /* Create an FTP Request */ FtpRequest = (FtpWebRequest)FtpWebRequest.Create(InterlocutorAddress + "/" + remoteFile); /* Log in to the FTP Server with the User Name and Password Provided */ FtpRequest.Credentials = new NetworkCredential(UserName, Password); LogInToInterlocutor(InterlocutorAddress); /* When in doubt, use these options */ FtpRequest.UseBinary = true; FtpRequest.UsePassive = true; FtpRequest.KeepAlive = true; /* Specify the Type of FTP Request */ FtpRequest.Method = WebRequestMethods.Ftp.UploadFile; /* Establish Return Communication with the FTP Server */ StartUploadData(localFile, remoteFile, InterlocutorAddress); FtpStream = FtpRequest.GetRequestStream(); /* Open a File Stream to Read the File for Upload */ FileStream localFileStream = new FileStream(localFile, FileMode.Create); /* Buffer for the Downloaded Data */ byte[] byteBuffer = new byte[BufferSize]; int bytesSent = localFileStream.Read(byteBuffer, 0, BufferSize); /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */ try { while (bytesSent != 0) { FtpStream.Write(byteBuffer, 0, bytesSent); bytesSent = localFileStream.Read(byteBuffer, 0, BufferSize); } } finally { /* Resource Cleanup */ localFileStream.Close(); EndUploadData(localFile, remoteFile, InterlocutorAddress); } } finally { FtpStream.Close(); FtpRequest = null; } }
/// <summary> Enviar un fichero al servidor Ftp. </summary> /// /// <param name="localFile"> Nombre del fichero local. </param> /// <param name="remoteFile"> Nombre del fichero remoto. </param> /// <param name="crearFichero"> Sobreescribir destino si ya existe. </param> /// /// <returns> Resultado (S/N/C) </returns> /// public Char FtpUploadFile(string localFile, string remoteFile, int crearFichero = (int)FileMode.Create) { FileStream localFileStream; byte[] byteBuffer; int bytesEnviados; Resultado = 'N'; try { if (String.IsNullOrEmpty(FTPHost) || string.IsNullOrEmpty(FTPUser) || FTPPwd == null) { UsrError = "KO - Faltan parámetros de conexión al Host Ftp"; UsrErrorC = -1; Resultado = 'N'; return(Resultado); } FtpRequest = (FtpWebRequest)FtpWebRequest.Create(FTPHost + "/" + remoteFile); FtpRequest.Credentials = new NetworkCredential(FTPUser, FTPPwd); FtpRequest.Method = WebRequestMethods.Ftp.UploadFile; // Canal de comunicación con el Host Ftp. FtpResponse = (FtpWebResponse)FtpRequest.GetResponse(); // Retorno de comunicación con el Host Ftp. FtpFileStream = FtpRequest.GetRequestStream(); // Canal de comunicación con el fichero local. localFileStream = new FileStream(localFile, FileMode.Open); byteBuffer = new byte[_BYTE_BUFFER]; bytesEnviados = localFileStream.Read(byteBuffer, 0, _BYTE_BUFFER); while (bytesEnviados > 0) { FtpFileStream.Write(byteBuffer, 0, bytesEnviados); bytesEnviados = localFileStream.Read(byteBuffer, 0, _BYTE_BUFFER); } // Cerrar canales de comunicación Ftp. localFileStream.Close(); FtpFileStream.Close(); FtpRequest = null; FtpResponse.Close(); Resultado = 'S'; UsrError = "OK"; } catch (Exception ex) { // Error de Ftp. UsrErrorC = ex.HResult; UsrError = ex.Message; UsrErrorE = ex.StackTrace; Resultado = 'C'; } return(Resultado); }