Пример #1
0
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="p"></param>
        /// <param name="fileStream"></param>
        /// <param name="processsing">上传过程中的处理事件</param>
        /// <param name="end">上传完成的处理事件</param>
        /// <param name="reserveObject">传入的保留参数对象</param>
        public void Upload(FtpParameter p, Stream fileStream, Action <FTPStatusData> processsing, Action <FTPStatusData> end, object reserveObject)
        {
            try
            {
                FtpCheckDirectoryExist(p);
                InitByParameters(p);

                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

                // UploadFile is not supported through an Http proxy
                // so we disable the proxy for this request.
                ftpRequest.Proxy = null;

                stream = ftpRequest.GetRequestStream();

                byte[] buffer = new byte[p.BufferSize];
                int    bytesRead;
                long   transfer  = 0;
                double speed     = 0;
                var    st        = DateTime.Now;
                var    totalbyte = fileStream.Length;
                while (true)
                {
                    bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    stream.Write(buffer, 0, bytesRead);

                    transfer += bytesRead;


                    if (processsing != null)
                    {
                        var fsd = new FTPStatusData();
                        fsd.CurrentByteLength    = bytesRead;
                        fsd.TransferedByteLength = transfer;
                        fsd.TotalByteLength      = totalbyte;
                        fsd.FileName             = Path.GetFileName(p.FTP_URL);
                        fsd.CostTime             = DateTime.Now - st;
                        var s = fsd.CostTime.TotalMilliseconds;
                        if (s != 0)
                        {
                            speed = (double)transfer / s * 1000;
                        }
                        fsd.Speed         = speed;
                        fsd.CurrentStatus = FTPStatusData.FtpStaus.Processing;
                        fsd.ReserveObject = reserveObject;

                        processsing(fsd);
                    }
                }

                // The request stream must be closed before getting
                // the response.
                stream.Close();

                ftpResponse =
                    (FtpWebResponse)ftpRequest.GetResponse();

                if (end != null)
                {
                    var fsd = new FTPStatusData();
                    fsd.CurrentByteLength    = bytesRead;
                    fsd.TransferedByteLength = transfer;
                    fsd.TotalByteLength      = totalbyte;
                    fsd.FileName             = Path.GetFileName(p.FTP_URL);
                    fsd.CostTime             = DateTime.Now - st;
                    var s = fsd.CostTime.TotalMilliseconds;
                    if (s != 0)
                    {
                        speed = (double)transfer / s * 1000;
                    }
                    fsd.Speed         = speed;
                    fsd.CurrentStatus = FTPStatusData.FtpStaus.End;
                    fsd.ReserveObject = reserveObject;

                    end(fsd);
                }
            }
            finally
            {
                Release();
            }
        }
Пример #2
0
        /// <summary>
        /// 下载文件,返回数据流
        /// </summary>
        /// <param name="p"></param>
        /// <param name="processsing"></param>
        /// <param name="end"></param>
        /// <param name="reserveObject"></param>
        /// <returns></returns>
        public MemoryStream DownLoadStream(FtpParameter p, Action <FTPStatusData> processsing, Action <FTPStatusData> end, object reserveObject)
        {
            try
            {
                string name = Common.ComFunc.FindFileName(p.FTP_URL);
                string dir  = p.FTP_URL;
                if (name.Length > 0)
                {
                    dir = dir.Replace(name, "");
                }

                //忽略文件名稱大小寫的處理
                //if (name.ToUpper() != name)
                //{
                //    name = FindMappingFileName(p);
                //}

                InitByParameters(p);

                ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                FtpWebResponse downloadResponse =
                    (FtpWebResponse)ftpRequest.GetResponse();

                stream = downloadResponse.GetResponseStream();

                string fileName =
                    Path.GetFileName(ftpRequest.RequestUri.AbsolutePath);

                MemoryStream rtn = null;
                if (fileName.Length == 0)
                {
                    rtn = new MemoryStream();
                }
                else
                {
                    //如果有指定文件名称
                    rtn = new MemoryStream();

                    byte[] buffer = new byte[p.BufferSize];
                    int    bytesRead;
                    long   transfer  = 0;
                    double speed     = 0;
                    var    st        = DateTime.Now;
                    var    totalbyte = fileStream.Length;
                    while (true)
                    {
                        bytesRead = stream.Read(buffer, 0, buffer.Length);
                        if (bytesRead == 0)
                        {
                            break;
                        }
                        rtn.Write(buffer, 0, bytesRead);

                        transfer += bytesRead;


                        if (processsing != null)
                        {
                            var fsd = new FTPStatusData();
                            fsd.CurrentByteLength    = bytesRead;
                            fsd.TransferedByteLength = transfer;
                            fsd.TotalByteLength      = totalbyte;
                            fsd.FileName             = Path.GetFileName(name);
                            fsd.CostTime             = DateTime.Now - st;
                            var s = fsd.CostTime.TotalMilliseconds;
                            if (s != 0)
                            {
                                speed = (double)transfer / s * 1000;
                            }
                            fsd.Speed         = speed;
                            fsd.CurrentStatus = FTPStatusData.FtpStaus.Processing;
                            fsd.ReserveObject = reserveObject;

                            processsing(fsd);
                        }
                    }

                    if (end != null)
                    {
                        var fsd = new FTPStatusData();
                        fsd.CurrentByteLength    = bytesRead;
                        fsd.TransferedByteLength = transfer;
                        fsd.TotalByteLength      = totalbyte;
                        fsd.FileName             = Path.GetFileName(name);
                        fsd.CostTime             = DateTime.Now - st;
                        var s = fsd.CostTime.TotalMilliseconds;
                        if (s != 0)
                        {
                            speed = (double)transfer / s * 1000;
                        }
                        fsd.Speed         = speed;
                        fsd.CurrentStatus = FTPStatusData.FtpStaus.End;
                        fsd.ReserveObject = reserveObject;

                        end(fsd);
                    }
                }

                return(rtn);
            }
            finally
            {
                Release();
            }
        }