Exemple #1
0
        /// <summary>
        /// Upload
        /// </summary>
        /// <param name="stream">Stream</param>
        /// <param name="remoteFile">Remote file</param>
        /// <param name="prog">Progress</param>
        /// <param name="cancel">Cancel</param>
        public void Upload(Stream stream, string remoteFile, IProgress prog, CancelEventArgs cancel)
        {
            long mx = stream.Length;

            if (prog != null)
            {
                prog.StartProgress(mx);
            }

            FtpWebRequest ftp = null;

            try
            {
                ftp = GetRequest(remoteFile, WebRequestMethods.Ftp.UploadFile);

                ftp.UseBinary     = true;
                ftp.ContentLength = mx;

                using (Stream sw = ftp.GetRequestStream())
                {
                    int      lee   = 0;
                    long     lleva = 0;
                    byte[]   bff   = new byte[BufferLength];
                    DateTime start = DateTime.Now;
                    bool     bts   = _UploadTimeOut != TimeSpan.Zero;

                    do
                    {
                        lee = stream.Read(bff, 0, bff.Length);
                        if (lee <= 0)
                        {
                            break;
                        }
                        lleva += lee;
                        sw.Write(bff, 0, lee);

                        if (prog != null)
                        {
                            prog.WriteProgress(lleva);
                        }

                        if (cancel != null && cancel.Cancel)
                        {
                            sw.Close();
                            ftp.Abort();

                            DeleteFile(remoteFile);
                        }

                        if (bts && DateTime.Now - start > _UploadTimeOut)
                        {
                            sw.Close();
                            ftp.Abort();

                            TimeSpan s = DateTime.Now - start;
                            throw (new Exception("TIMEOUT EXCEDED ( AVG " +
                                                 StringHelper.Convert2Kb(lleva / s.TotalSeconds) + "/sec )"));
                        }
                    } while (lee > 0);
                    sw.Close();
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
            finally
            {
                if (prog != null)
                {
                    prog.EndProgress();
                }
                if (ftp != null)
                {
                    ftp.Abort();
                }
            }

            long mxhay = GetFileSize(remoteFile);

            if (mxhay != 0)
            {
                if (mx != mxhay)
                {
                    throw (new Exception("Length error"));
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Download
        /// </summary>
        /// <param name="remoteFile">Remote file</param>
        /// <param name="stream">Stream</param>
        /// <param name="prog">Progress</param>
        /// <param name="cancel">Cancel</param>
        public void Download(string remoteFile, Stream stream, IProgress prog, CancelEventArgs cancel)
        {
            if (prog != null)
            {
                prog.StartProgress(GetFileSize(remoteFile));
            }

            FtpWebRequest ftp = null;

            try
            {
                ftp           = GetRequest(remoteFile, WebRequestMethods.Ftp.DownloadFile);
                ftp.UseBinary = true;
                using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
                    using (BinaryReader sr = new BinaryReader(response.GetResponseStream()))
                    {
                        int      lee   = 0;
                        long     lleva = 0;
                        byte[]   bff   = new byte[BufferLength];
                        DateTime start = DateTime.Now;
                        bool     bts   = _DownloadTimeOut != TimeSpan.Zero;

                        do
                        {
                            lee = sr.Read(bff, 0, bff.Length);
                            if (lee == 0)
                            {
                                break;
                            }
                            lleva += lee;
                            stream.Write(bff, 0, lee);

                            if (prog != null)
                            {
                                prog.WriteProgress(lleva);
                            }

                            if (cancel != null && cancel.Cancel)
                            {
                                sr.Close();
                                response.Close();
                                ftp.Abort();

                                return;
                            }

                            if (bts && DateTime.Now - start > _DownloadTimeOut)
                            {
                                sr.Close();
                                response.Close();
                                ftp.Abort();

                                TimeSpan s = DateTime.Now - start;
                                throw (new Exception("TIMEOUT EXCEDED ( AVG " + StringHelper.Convert2Kb(lleva / s.TotalSeconds) + "/sec )"));
                            }
                        } while (lee > 0);

                        sr.Close();
                        response.Close();
                    }
            }
            catch (Exception e)
            {
                throw (e);
            }
            finally
            {
                if (prog != null)
                {
                    prog.EndProgress();
                }
                if (ftp != null)
                {
                    ftp.Abort();
                }
            }
        }