Exemplo n.º 1
0
        private FtpStatusCode beginUploadFile1(ref ftpUDParams ftpParam)
        {
            FtpStatusCode  ftpStatusCode    = FtpStatusCode.Undefined;
            string         path             = ftpParam.path;
            string         filename         = ftpParam.filename;
            FtpWebResponse ftpWebResponse   = (FtpWebResponse)null;
            Stream         stream           = (Stream)null;
            FileStream     fileStream       = (FileStream)null;
            string         requestUriString = string.Format("ftp://{0}/{1}/{2}", (object)this._FtpServerIP, (object)path, (object)filename.Substring(filename.LastIndexOf("\\") + 1));

            try
            {
                FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUriString);
                ftpWebRequest.KeepAlive   = false;
                ftpWebRequest.Credentials = (ICredentials) new NetworkCredential(this._UserName, this._Password);
                ftpWebRequest.Method      = "STOR";
                ftpWebRequest.UseBinary   = true;
                stream     = ftpWebRequest.GetRequestStream();
                fileStream = new FileStream(filename, FileMode.Open);
                byte[] buffer = new byte[1024];
                while (true)
                {
                    int count = fileStream.Read(buffer, 0, buffer.Length);
                    if (count != 0)
                    {
                        stream.Write(buffer, 0, count);
                    }
                    else
                    {
                        break;
                    }
                }
                this.ftpUploadFileSuccess.BeginInvoke(filename, (AsyncCallback)null, (object)null);
                stream.Close();
                ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
                ftpStatusCode  = ftpWebResponse.StatusCode;
            }
            catch (Exception ex)
            {
                this.ftpUploadFileFailed.BeginInvoke(filename, ex.Message, (AsyncCallback)null, (object)null);
            }
            finally
            {
                if (ftpWebResponse != null)
                {
                    ftpWebResponse.Close();
                }
                if (fileStream != null)
                {
                    fileStream.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return(ftpStatusCode);
        }
Exemplo n.º 2
0
 private void uploadfile(ref ftpUDParams ftpParam)
 {
     this.uploadFile = new FtpConnect.UploadFileEventHandler(this.beginUploadFile1);
     this.uploadFile.BeginInvoke(ref ftpParam, (AsyncCallback)null, (object)null);
 }
Exemplo n.º 3
0
        private FtpStatusCode beginUploadFile3(ftpUDParams ftpParam)
        {
            FtpStatusCode statuscode = FtpStatusCode.Undefined;
            string        path       = ftpParam.path;
            string        fileName   = ftpParam.filename;

            new Thread((ThreadStart)(() =>
            {
                FtpWebResponse ftpWebResponse = (FtpWebResponse)null;
                Stream stream = (Stream)null;
                FileStream fileStream = (FileStream)null;
                string requestUriString = string.Format("ftp://{0}/{1}", (object)this._FtpServerIP, (object)fileName.Substring(fileName.LastIndexOf("\\") + 1));
                try
                {
                    FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUriString);
                    ftpWebRequest.KeepAlive = false;
                    ftpWebRequest.Credentials = (ICredentials) new NetworkCredential(this._UserName, this._Password);
                    ftpWebRequest.Method = "STOR";
                    stream = ftpWebRequest.GetRequestStream();
                    fileStream = new FileStream(fileName, FileMode.Open);
                    int length1 = 1048576;
                    int num = (int)(fileStream.Length / (long)length1);
                    int length2 = (int)(fileStream.Length % (long)length1);
                    ftpParam.progressBar.BeginInvoke((Delegate)this.processBarMaxmum, (object)ftpParam.progressBar, (object)num);
                    if (length2 != 0)
                    {
                        ftpParam.progressBar.BeginInvoke((Delegate)this.processBarMaxmum, (object)ftpParam.progressBar, (object)(num + 1));
                    }
                    for (int index = 0; index < num; ++index)
                    {
                        byte[] buffer = new byte[length1];
                        fileStream.Read(buffer, 0, buffer.Length);
                        stream.Write(buffer, 0, buffer.Length);
                        string str = (Convert.ToDouble(index) / Convert.ToDouble(num) * 100.0).ToString() + "%";
                        ftpParam.progressBar.BeginInvoke((Delegate)this.processBarValue, (object)ftpParam.progressBar, (object)ftpParam.label, (object)index, (object)str);
                    }
                    if (length2 != 0)
                    {
                        byte[] buffer = new byte[length2];
                        fileStream.Read(buffer, 0, buffer.Length);
                        stream.Write(buffer, 0, buffer.Length);
                        ftpParam.progressBar.BeginInvoke((Delegate)this.processBarValue, (object)ftpParam.progressBar, (object)ftpParam.label, (object)(num + 1), (object)"100%");
                    }
                    this.ftpUploadFileSuccess.BeginInvoke(fileName, (AsyncCallback)null, (object)null);
                    stream.Close();
                    ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
                    statuscode = ftpWebResponse.StatusCode;
                }
                catch (Exception ex)
                {
                    this.ftpUploadFileFailed.BeginInvoke(fileName, ex.Message, (AsyncCallback)null, (object)null);
                }
                finally
                {
                    if (ftpWebResponse != null)
                    {
                        ftpWebResponse.Close();
                    }
                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }
            })).Start();
            return(statuscode);
        }
Exemplo n.º 4
0
        public FtpStatusCode beginUploadFile(ftpUDParams ftpParam)
        {
            FtpStatusCode statuscode = FtpStatusCode.Undefined;
            string        path       = ftpParam.path;
            string        fileName   = ftpParam.filename;

            new Thread((ThreadStart)(() =>
            {
                string requestUriString = string.Format("ftp://{0}/{1}/{2}", (object)this._FtpServerIP, (object)path, (object)fileName.Substring(fileName.LastIndexOf("\\") + 1));
                FtpWebRequest ftpWebRequest;
                Stream requestStream;
                try
                {
                    ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUriString);
                    ftpWebRequest.Credentials = (ICredentials) new NetworkCredential(this._UserName, this._Password);
                    ftpWebRequest.Method = "STOR";
                    ftpWebRequest.UseBinary = true;
                    requestStream = ftpWebRequest.GetRequestStream();
                }
                catch (Exception ex)
                {
                    this.ftpConnectFailed.BeginInvoke(fileName, ex.Message, (AsyncCallback)null, (object)null);
                    return;
                }
                FileStream fileStream = new FileStream(fileName, FileMode.Open);
                if (ftpParam.progressBar != null)
                {
                    int length1 = 1048576;
                    int num = (int)(fileStream.Length / (long)length1);
                    int length2 = (int)(fileStream.Length % (long)length1);
                    ftpParam.progressBar.BeginInvoke((Delegate)this.processBarMaxmum, (object)ftpParam.progressBar, (object)num);
                    if (length2 != 0)
                    {
                        ftpParam.progressBar.BeginInvoke((Delegate)this.processBarMaxmum, (object)ftpParam.progressBar, (object)(num + 1));
                    }
                    for (int index = 0; index < num; ++index)
                    {
                        byte[] buffer = new byte[length1];
                        fileStream.Read(buffer, 0, buffer.Length);
                        requestStream.Write(buffer, 0, buffer.Length);
                        string str = (Convert.ToDouble(index) / Convert.ToDouble(num) * 100.0).ToString() + "%";
                        ftpParam.progressBar.BeginInvoke((Delegate)this.processBarValue, (object)ftpParam.progressBar, (object)ftpParam.label, (object)index, (object)str);
                    }
                    if (length2 != 0)
                    {
                        byte[] buffer = new byte[length2];
                        fileStream.Read(buffer, 0, buffer.Length);
                        requestStream.Write(buffer, 0, buffer.Length);
                        ftpParam.progressBar.BeginInvoke((Delegate)this.processBarValue, (object)ftpParam.progressBar, (object)ftpParam.label, (object)(num + 1), (object)"100%");
                    }
                }
                else
                {
                    byte[] buffer = new byte[1024];
                    while (true)
                    {
                        int count = fileStream.Read(buffer, 0, buffer.Length);
                        if (count != 0)
                        {
                            requestStream.Write(buffer, 0, count);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                if (this.clientConnect != null && this.clientConnect._tcpConnectState && ftpParam.msg != null)
                {
                    this.clientConnect.SendMessage(ftpParam.msg);
                }
                requestStream.Close();
                FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse();
                statuscode = response.StatusCode;
                if (fileStream != null)
                {
                    fileStream.Dispose();
                    fileStream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
                if (this.ftpUploadFileSuccess != null)
                {
                    this.ftpUploadFileSuccess.BeginInvoke(fileName, (AsyncCallback)null, (object)null);
                }
                Thread.CurrentThread.Abort();
            }))
            {
                IsBackground = true
            }.Start();
            return(statuscode);
        }
Exemplo n.º 5
0
        private FtpStatusCode beginUploadFile2(ref ftpUDParams ftpParam)
        {
            FtpStatusCode  ftpStatusCode    = FtpStatusCode.Undefined;
            string         path             = ftpParam.path;
            string         filename         = ftpParam.filename;
            FtpWebResponse ftpWebResponse   = (FtpWebResponse)null;
            Stream         stream           = (Stream)null;
            FileStream     fileStream       = (FileStream)null;
            string         requestUriString = string.Format("ftp://{0}/{1}/{2}", (object)this._FtpServerIP, (object)path, (object)filename.Substring(filename.LastIndexOf("\\") + 1));

            try
            {
                FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUriString);
                ftpWebRequest.KeepAlive   = false;
                ftpWebRequest.Credentials = (ICredentials) new NetworkCredential(this._UserName, this._Password);
                ftpWebRequest.Method      = "STOR";
                ftpWebRequest.UseBinary   = true;
                stream     = ftpWebRequest.GetRequestStream();
                fileStream = new FileStream(filename, FileMode.Open);
                int length1 = 1048576;
                int num1    = (int)(fileStream.Length / (long)length1);
                int length2 = (int)(fileStream.Length % (long)length1);
                ftpParam.progressBar.Maximum = num1;
                if (length2 != 0)
                {
                    ++ftpParam.progressBar.Maximum;
                }
                for (int index = 0; index < num1; ++index)
                {
                    byte[] buffer = new byte[length1];
                    fileStream.Read(buffer, 0, buffer.Length);
                    stream.Write(buffer, 0, buffer.Length);
                    ftpParam.progressBar.Value = index;
                    double num2 = Convert.ToDouble(index) / Convert.ToDouble(num1) * 100.0;
                    ftpParam.label.Text = num2.ToString() + "%";
                }
                if (length2 != 0)
                {
                    byte[] buffer = new byte[length2];
                    fileStream.Read(buffer, 0, buffer.Length);
                    stream.Write(buffer, 0, buffer.Length);
                    ++ftpParam.progressBar.Value;
                }
                this.ftpUploadFileSuccess.BeginInvoke(filename, (AsyncCallback)null, (object)null);
                stream.Close();
                ftpWebResponse = (FtpWebResponse)ftpWebRequest.GetResponse();
                ftpStatusCode  = ftpWebResponse.StatusCode;
            }
            catch (Exception ex)
            {
                this.ftpUploadFileFailed.BeginInvoke(filename, ex.Message, (AsyncCallback)null, (object)null);
            }
            finally
            {
                if (ftpWebResponse != null)
                {
                    ftpWebResponse.Close();
                }
                if (fileStream != null)
                {
                    fileStream.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
            }
            return(ftpStatusCode);
        }