Beispiel #1
0
        public void DownloadIntoFolderTest()
        {
            string        targetFolderPath = Path.Combine(Path.GetTempPath(), "downloader test folder");
            DirectoryInfo targetFolder     = new DirectoryInfo(targetFolderPath);

            DownloadConfiguration config = new DownloadConfiguration {
                BufferBlockSize       = 1024,
                ChunkCount            = 1,
                ParallelDownload      = false,
                MaxTryAgainOnFailover = 100,
                OnTheFlyDownload      = true
            };
            DownloadService downloader = new DownloadService(config);

            downloader.DownloadFileAsync(DownloadTestHelper.File1KbUrl, targetFolder).Wait();
            Assert.AreEqual(DownloadTestHelper.FileSize1Kb, downloader.Package.TotalFileSize);
            downloader.Clear();
            downloader.DownloadFileAsync(DownloadTestHelper.File150KbUrl, targetFolder).Wait();
            Assert.AreEqual(DownloadTestHelper.FileSize150Kb, downloader.Package.TotalFileSize);
            downloader.Clear();

            Assert.IsTrue(targetFolder.Exists);
            FileInfo[] downloadedFiles = targetFolder.GetFiles();
            long       totalSize       = downloadedFiles.Sum(file => file.Length);

            Assert.AreEqual(DownloadTestHelper.FileSize1Kb + DownloadTestHelper.FileSize150Kb, totalSize);
            Assert.IsTrue(downloadedFiles.Any(file => file.Name == DownloadTestHelper.File1KbName));
            Assert.IsTrue(downloadedFiles.Any(file => file.Name == DownloadTestHelper.File150KbName));

            targetFolder.Delete(true);
        }
Beispiel #2
0
        static async Task Main(string[] args)
        {
            var chunkCount = 8;

            DownloadList = File.Exists(DownloadListFile)
                ? JsonConvert.DeserializeObject <List <DownloadItem> >(File.ReadAllText(DownloadListFile))
                : null;

            DownloadList ??= new List <DownloadItem>
            {
                new DownloadItem {
                    FileName = Path.Combine(Path.GetTempPath(), "100MB.zip"), Url = "http://ipv4.download.thinkbroadband.com/100MB.zip"
                }
            };

            var options = new ProgressBarOptions
            {
                ForegroundColor     = ConsoleColor.Green,
                ForegroundColorDone = ConsoleColor.DarkGreen,
                BackgroundColor     = ConsoleColor.DarkGray,
                BackgroundCharacter = '\u2593'
            };

            ChildOption = new ProgressBarOptions
            {
                ForegroundColor   = ConsoleColor.Yellow,
                BackgroundColor   = ConsoleColor.DarkGray,
                ProgressCharacter = '─'
            };

            var downloadOpt = new DownloadConfiguration()
            {
                ParallelDownload      = true,         // download parts of file as parallel or not
                BufferBlockSize       = 10240,        // usually, hosts support max to 8000 bytes
                ChunkCount            = chunkCount,   // file parts to download
                MaxTryAgainOnFailover = int.MaxValue, // the maximum number of times to fail.
                OnTheFlyDownload      = true,         // caching in-memory mode
                Timeout = 1000                        // timeout (millisecond) per stream block reader
            };
            var ds = new DownloadService(downloadOpt);

            ds.ChunkDownloadProgressChanged += OnChunkDownloadProgressChanged;
            ds.DownloadProgressChanged      += OnDownloadProgressChanged;
            ds.DownloadFileCompleted        += OnDownloadFileCompleted;

            foreach (var downloadItem in DownloadList)
            {
                Console.Clear();
                ConsoleProgress        = new ProgressBar(10000, $"Downloading {Path.GetFileName(downloadItem.FileName)} file", options);
                ChildConsoleProgresses = new ConcurrentDictionary <string, ChildProgressBar>();

                await ds.DownloadFileAsync(downloadItem.Url, downloadItem.FileName).ConfigureAwait(false);

                ds.Clear();
            }
        }
Beispiel #3
0
        private static async Task DownloadAll(IEnumerable <DownloadItem> downloadList)
        {
            foreach (DownloadItem downloadItem in downloadList)
            {
                // begin download from url
                DownloadService ds = await DownloadFile(downloadItem).ConfigureAwait(false);

                // clear download to order new of one
                ds.Clear();
            }
        }
Beispiel #4
0
        private static async Task DownloadList(IEnumerable <DownloadItem> downloadList, DownloadConfiguration config)
        {
            foreach (DownloadItem downloadItem in downloadList)
            {
                // begin download from url
                DownloadService ds = await DownloadFile(downloadItem, config);

                await Task.Delay(1000);

                // clear download to order new of one
                ds.Clear();
            }
        }