Exemplo n.º 1
0
        private bool DownloadSingleFileFTP(ItemInfo file, String dstDir)
        {
            bool isSuccess = true;
            Byte[] readBytes = new Byte[PacketSize];

            // create destination directory if not exits
            String dir = Path.GetDirectoryName(Path.Combine(dstDir, file.FullPath));
            if (!Directory.Exists(dir) || !string.IsNullOrEmpty(dir))
            {
                Directory.CreateDirectory(dir);
            }

            FtpWebRequest webReq;
            FtpWebResponse webResp = null;

            try
            {
                webReq = (FtpWebRequest)WebRequest.Create(file.DownloadUrl);
                webReq.Credentials = updater.GetCredential();
                webReq.KeepAlive = false;
                webReq.UsePassive = true;
                webReq.Method = updater.IsFtp ? WebRequestMethods.Ftp.DownloadFile : WebRequestMethods.Http.Get;
                
                webReq.Credentials = updater.GetCredential();
                webResp = (FtpWebResponse)webReq.GetResponse();

                downloadui.SetDownloadingFileName(file.FileName);

                using (FileStream writer = new FileStream(Path.Combine(dir, file.FileName), FileMode.Create))
                {
                    int currentFileProgress = 0;

                    while (currentFileProgress < file.FileSize && !downloadWorker.CancellationPending)
                    {
                        // while (this.IsPaused) { System.Threading.Thread.Sleep(50); }

                        var responseStream = webResp.GetResponseStream();

                        if (responseStream != null)
                        {
                            Int32 currentPacketSize = responseStream
                                .Read(readBytes, 0, PacketSize);

                            currentFileProgress += currentPacketSize;
                            TotalProgress += currentPacketSize;
                            double percent = (TotalProgress / (updater.TotalFileSize * 1.0)) * 100;

                            downloadWorker.ReportProgress((int)percent);

                            writer.Write(readBytes, 0, currentPacketSize);
                        }
                    }

                    writer.Flush();
                    writer.Close();
                }

                webResp.Close();
            }
            catch (Exception ex)
            {
                isSuccess = false;
                Log.Error(ex.Message);
                Log.Debug(ex.StackTrace);
            }

            return isSuccess;
        }
Exemplo n.º 2
0
        private ItemInfo GetItemInfoDetail(String url, ItemInfo it)
        {
            WebRequest request = WebRequest.Create(url);

            request.Method = (this.Server.Scheme.StartsWith("FTP",StringComparison.InvariantCultureIgnoreCase))
                ? WebRequestMethods.Ftp.GetFileSize : WebRequestMethods.Http.Head;

            request.Credentials = new NetworkCredential(this.Username, this.Password);

            ItemInfo item = new ItemInfo();

            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    item.FileSize = response.ContentLength;

                    item.ContentType = response.ContentType;
                }
            }
            catch (WebException ex) { }
            catch (Exception ex) { }

            item.DownloadUrl = request.RequestUri.ToString();
            item.FullPath =  it.FileName;
            item.FileName = Path.GetFileName(it.FileName);
            
            return item;
        }