Ejemplo n.º 1
0
        private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
        {
            // Displays the operation identifier, and the transfer progress.

            Console.WriteLine("Downloaded {0}mbs",
                              ((int)e.BytesReceived / 1000000)
                              );
            string percentage = ((int)e.BytesReceived / 1000000).ToString();

            if (OnFileProgress != null)
            {
                OnFileProgress.Invoke(this, new DownloadProgress(percentage));
            }
        }
Ejemplo n.º 2
0
        private async Task CreateDownload(string url, string pathToSave, string id)
        {
            try
            {
                int fileSize = await GetFileSize(url);

                var finalPathToSave = pathToSave + "\\" + Path.GetFileName(url);
                using (var httpClient = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(HttpMethod.Get, url))
                    {
                        using (
                            Stream contentStream = await(await httpClient.SendAsync(request)).Content.ReadAsStreamAsync(),
                            stream = new FileStream(finalPathToSave, FileMode.Create))
                        {
                            var buffer     = new byte[4096];
                            var downloaded = 0;
                            while (true)
                            {
                                var length = contentStream.Read(buffer, 0, buffer.Length);
                                if (length <= 0)
                                {
                                    break;
                                }

                                await stream.WriteAsync(buffer, 0, length);

                                downloaded += length;
                                if (downloaded % 102400 == 0)
                                {
                                    Task.Factory.StartNew(() => OnFileProgress?.Invoke(id, fileSize, downloaded)).Wait();
                                }
                            }
                        }
                    }
                }
                OnDownloaded?.Invoke(id);
            }
            catch (Exception ex)
            {
                OnFailed?.Invoke(id, ex);
            }
            ManageDownloads();
        }
Ejemplo n.º 3
0
        public void AddFileToDownloadingQueue(string fileId, string url, string pathToSave)
        {
            try
            {
                DownloadQueue.Enqueue(fileId);
                var download = new Download(fileId, url, pathToSave, CreateDownload(url, pathToSave, fileId));

                if (!AllDownloads.TryAdd(fileId, download))
                {
                    throw new Exception($"An exception while adding {fileId}");
                }
                Task.Factory.StartNew(() => OnFileProgress?.Invoke(fileId, 0, 0)).Wait();
            }
            catch (Exception ex)
            {
                OnDownloaderException?.Invoke(ex);
            }
            ManageDownloads();
        }