private void DownloadContent(DownloadableContent content)
        {
            var request = (HttpWebRequest)WebRequest.Create(content.DownloadUrl);

            if (this.BytesToDownload.HasValue)
            {
                request.AddRange(0, this.BytesToDownload.Value - 1);
            }

            // the following code is alternative, you may implement the function after your needs
            using (WebResponse response = request.GetResponse())
            {
                using (Stream source = response.GetResponseStream())
                {
                    var directory = content.SavedFolder;
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }

                    var filePath = Utilities.NormalizeFilePath(Path.Combine(directory, content.FullName));
                    using (FileStream target = File.Open(filePath, FileMode.Create, FileAccess.Write))
                    {
                        var  buffer = new byte[1024];
                        bool cancel = false;
                        int  bytes;
                        int  copiedBytes = 0;

                        while (!cancel && (bytes = source.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            target.Write(buffer, 0, bytes);

                            copiedBytes += bytes;

                            var eventArgs = new ProgressEventArgs((copiedBytes * 1.0 / response.ContentLength) * 100);

                            if (this.DownloadProgressChanged != null)
                            {
                                this.DownloadProgressChanged(this, eventArgs);

                                if (eventArgs.Cancel)
                                {
                                    cancel           = true;
                                    this.IsCancelled = true;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        private void DownloadContent(DownloadableContent content)
        {
            var request = (HttpWebRequest)WebRequest.Create(content.DownloadUrl);

            if (this.BytesToDownload.HasValue)
            {
                request.AddRange(0, this.BytesToDownload.Value - 1);
            }

            // the following code is alternative, you may implement the function after your needs
            using (WebResponse response = request.GetResponse())
            {
                using (Stream source = response.GetResponseStream())
                {
                    var directory = content.SavedFolder;
                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }

                    var filePath = Utilities.NormalizeFilePath(Path.Combine(directory, content.FullName));
                    using (FileStream target = File.Open(filePath, FileMode.Create, FileAccess.Write))
                    {
                        var buffer = new byte[1024];
                        bool cancel = false;
                        int bytes;
                        int copiedBytes = 0;

                        while (!cancel && (bytes = source.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            target.Write(buffer, 0, bytes);

                            copiedBytes += bytes;

                            var eventArgs = new ProgressEventArgs((copiedBytes * 1.0 / response.ContentLength) * 100);

                            if (this.DownloadProgressChanged != null)
                            {
                                this.DownloadProgressChanged(this, eventArgs);

                                if (eventArgs.Cancel)
                                {
                                    cancel = true;
                                    this.IsCancelled = true;
                                }
                            }
                        }
                    }
                }
            }
        }