Пример #1
0
        /// <summary>
        /// Sube un archivo al servidor remoto.-
        /// </summary>
        /// <param name="fileName">Nombre del archivo a subir</param>
        /// <param name="resume">Llama al comando REST: Reiniciar cargas y descargas de FTP y despues RETR
        /// Las descargas se pueden reiniciar emitiendo primero un comando rest con el desplazamiento deseado y, 
        /// a continuación, emitiendo el comando retr.
        /// </param>
        public void Upload(string fileName, Boolean resume)
        {

            if (!logined)
            {
                Conect();
            }
            Socket cSocket = null;
            try
            {
                cSocket = CreateDataSocket();
            }
            catch (IOException ex)
            {
             
                SendErrorEvent(ex);
                throw ex;
            }
            long offset = 0;

            if (resume)
            {

                try
                {
                    SetBinaryMode(true);
                    offset = GetFileSize(fileName);
                }
                catch (Exception)
                {
                    offset = 0;
                }
            }

            if (offset > 0)
            {
                SendCommand("REST " + offset);
                if (retValue != 350)
                {
                    //throw new IOException(reply.Substring(4));
                    offset = 0;
                }
            }

            SendCommand("STOR " + Path.GetFileName(fileName));

            if (!(retValue == 125 || retValue == 150))
            {
                SendErrorEvent(new IOException(reply.Substring(4)));
                throw new IOException(reply.Substring(4));
               
            }

            // Abre el stream de enrtada para leer el archivo de origen
            FileStream input = new FileStream(fileName, FileMode.Open);

            if (offset != 0)
            {
             
                input.Seek(offset, SeekOrigin.Begin);
            }

            SendDebug(string.Concat("Uploading file ", fileName, " to " + ftpPath));

            while ((bytes = input.Read(buffer, 0, buffer.Length)) > 0)
            {

                cSocket.Send(buffer, bytes, 0);

            }
            input.Close();


            if (cSocket.Connected)
            {
                cSocket.Close();
            }

            ReadReply();
            if (!(retValue == 226 || retValue == 250))
            {
                SendErrorEvent(new IOException(reply.Substring(4)));
                throw new IOException(reply.Substring(4));
            }
    

            SendDebug(string.Concat("File ", fileName, " must successfully uploaded to ", ftpServer, "/", ftpPath));
            if (OnUploadEvent != null)
            {
                UploadData data = new UploadData();
                data.FtpComponent = this;
                data.Message = string.Concat("File ", fileName, " must successfully uploaded to ", ftpServer, "/", ftpPath);
                OnUploadEvent(data, null);
            }
        }
Пример #2
0
        /// <summary>
        /// Sube un archivo al servidor remoto.-
        /// </summary>
        /// <param name="fileName">Nombre del archivo a subir</param>
        /// <param name="resume">Llama al comando REST: Reiniciar cargas y descargas de FTP y despues RETR
        /// Las descargas se pueden reiniciar emitiendo primero un comando rest con el desplazamiento deseado y,
        /// a continuación, emitiendo el comando retr.
        /// </param>
        public void Upload(string fileName, Boolean resume)
        {
            if (!logined)
            {
                Conect();
            }
            Socket cSocket = null;

            try
            {
                cSocket = CreateDataSocket();
            }
            catch (IOException ex)
            {
                SendErrorEvent(ex);
                throw ex;
            }
            long offset = 0;

            if (resume)
            {
                try
                {
                    SetBinaryMode(true);
                    offset = GetFileSize(fileName);
                }
                catch (Exception)
                {
                    offset = 0;
                }
            }

            if (offset > 0)
            {
                SendCommand("REST " + offset);
                if (retValue != 350)
                {
                    //throw new IOException(reply.Substring(4));
                    offset = 0;
                }
            }

            SendCommand("STOR " + Path.GetFileName(fileName));

            if (!(retValue == 125 || retValue == 150))
            {
                SendErrorEvent(new IOException(reply.Substring(4)));
                throw new IOException(reply.Substring(4));
            }

            // Abre el stream de enrtada para leer el archivo de origen
            FileStream input = new FileStream(fileName, FileMode.Open);

            if (offset != 0)
            {
                input.Seek(offset, SeekOrigin.Begin);
            }

            SendDebug(string.Concat("Uploading file ", fileName, " to " + ftpPath));

            while ((bytes = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                cSocket.Send(buffer, bytes, 0);
            }
            input.Close();


            if (cSocket.Connected)
            {
                cSocket.Close();
            }

            ReadReply();
            if (!(retValue == 226 || retValue == 250))
            {
                SendErrorEvent(new IOException(reply.Substring(4)));
                throw new IOException(reply.Substring(4));
            }


            SendDebug(string.Concat("File ", fileName, " must successfully uploaded to ", ftpServer, "/", ftpPath));
            if (OnUploadEvent != null)
            {
                UploadData data = new UploadData();
                data.FtpComponent = this;
                data.Message      = string.Concat("File ", fileName, " must successfully uploaded to ", ftpServer, "/", ftpPath);
                OnUploadEvent(data, null);
            }
        }