Ejemplo n.º 1
0
        protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
        {
            try
            {
                if (DownloadData != null)
                {
                    DownloadData.Close();
                }
            }
            catch { }

            try
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
            catch { }

            if (e.Cancelled)
            {
                DownloadStatus = DownloadStatus.Canceled;
            }
            else if (e.Error != null)
            {
                DownloadStatus = DownloadStatus.Failed;
            }
            else
            {
                DownloadStatus = DownloadStatus.Success;
            }

            DownloadSpeed = 0;

            base.OnRunWorkerCompleted(e);
        }
Ejemplo n.º 2
0
        public static DownloadData Create(string url, string destFolder, String fileName, IWebProxy proxy)
        {
            // This is what we will return
            DownloadData downloadData = new DownloadData();
            downloadData.proxy = proxy;

            long urlSize = downloadData.GetFileSize(url);
            downloadData.size = urlSize;

            WebRequest req = downloadData.GetRequest(url);
            try
            {
                downloadData.response = (WebResponse)req.GetResponse();
            }
            catch (Exception e)
            {
                throw new ArgumentException(String.Format(
                    "Error downloading \"{0}\": {1}", url, e.Message), e);
            }

            // Check to make sure the response isn't an error. If it is this method
            // will throw exceptions.
            ValidateResponse(downloadData.response, url);

            String downloadTo = Path.Combine(destFolder, fileName);

            // If we don't know how big the file is supposed to be,
            // we can't resume, so delete what we already have if something is on disk already.
            if (!downloadData.IsProgressKnown && File.Exists(downloadTo))
                File.Delete(downloadTo);

            if (downloadData.IsProgressKnown && File.Exists(downloadTo))
            {
                // We only support resuming on http requests
                if (!(downloadData.Response is HttpWebResponse))
                {
                    File.Delete(downloadTo);
                }
                else
                {
                    // Try and start where the file on disk left off
                    downloadData.start = new FileInfo(downloadTo).Length;

                    // If we have a file that's bigger than what is online, then something
                    // strange happened. Delete it and start again.
                    if (downloadData.start > urlSize)
                        File.Delete(downloadTo);
                    else if (downloadData.start < urlSize)
                    {
                        // Try and resume by creating a new request with a new start position
                        downloadData.response.Close();
                        req = downloadData.GetRequest(url);
                        ((HttpWebRequest)req).AddRange((int)downloadData.start);
                        downloadData.response = req.GetResponse();

                        if (((HttpWebResponse)downloadData.Response).StatusCode != HttpStatusCode.PartialContent)
                        {
                            // They didn't support our resume request.
                            File.Delete(downloadTo);
                            downloadData.start = 0;
                        }
                    }
                }
            }
            return downloadData;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Begin downloading the file at the specified url, and save it to the given folder.
        /// </summary>
        private void Download(object sender, DoWorkEventArgs e)
        {
            _pause         = false;
            DownloadStatus = DownloadStatus.Downloading;
            OnProgressChanged(new ProgressChangedEventArgs(Progress, null));
            DownloadData = DownloadData.Create(FileUrl, DestFolder, this.DestFileName, Helper.InitialProxy());

            if (string.IsNullOrEmpty(DestFileName))
            {
                Path.GetFileName(DownloadData.Response.ResponseUri.ToString());
            }

            this.downloadingTo = Path.Combine(DestFolder, DestFileName);
            FileMode mode = DownloadData.StartPoint > 0 ? FileMode.Append : FileMode.OpenOrCreate;

            fileStream = File.Open(downloadingTo, mode, FileAccess.Write);
            byte[] buffer = new byte[DownloadBlockSize];
            totalDownloaded = DownloadData.StartPoint;
            double totalDownloadedInTime = 0; long totalDownloadedTime = 0;

            OnProgressChanged(new ProgressChangedEventArgs(Progress, null));
            bool callProgess = true;

            while (true)
            {
                callProgess = true;

                if (this.CancellationPending)
                {
                    DownloadSpeed = Progress = 0;
                    e.Cancel      = true;
                    break;
                }

                if (_pause)
                {
                    DownloadSpeed  = 0;
                    DownloadStatus = DownloadStatus.Paused;
                    System.Threading.Thread.Sleep(500);
                }
                else
                {
                    DownloadStatus = DownloadStatus.Downloading;
                    long startTime = DateTime.Now.Ticks;
                    int  readCount = DownloadData.DownloadStream.Read(buffer, 0, DownloadBlockSize);

                    if (readCount == 0)
                    {
                        break;
                    }

                    totalDownloadedInTime += readCount;
                    totalDownloadedTime   += DateTime.Now.Ticks - startTime;

                    if (callProgess = totalDownloadedTime >= SecondTicks)
                    {
                        DownloadSpeed         = (int)(totalDownloadedInTime / TimeSpan.FromTicks(totalDownloadedTime).TotalSeconds);
                        totalDownloadedInTime = 0; totalDownloadedTime = 0;
                    }

                    totalDownloaded += readCount;
                    fileStream.Write(buffer, 0, readCount);
                    fileStream.Flush();
                }

                Progress = (int)(100.0 * totalDownloaded / DownloadData.FileSize);

                if (callProgess && DownloadData.IsProgressKnown)
                {
                    ReportProgress(Progress);
                }
            }

            ReportProgress(Progress);
        }
Ejemplo n.º 4
0
        public static DownloadData Create(string url, string destFolder, string fileName, IWebProxy proxy)
        {
            // This is what we will return
            DownloadData downloadData = new DownloadData();

            downloadData.proxy = proxy;

            long urlSize = downloadData.GetFileSize(url);

            downloadData.size = urlSize;

            WebRequest req = downloadData.GetRequest(url);

            try
            {
                downloadData.response = (WebResponse)req.GetResponse();
            }
            catch (Exception e)
            {
                throw new ArgumentException(string.Format("Error downloading \"{0}\": {1}", url, e.Message), e);
            }

            // Check to make sure the response isn't an error. If it is this method
            // will throw exceptions.
            ValidateResponse(downloadData.response, url);

            string downloadTo = Path.Combine(destFolder, fileName);

            // If we don't know how big the file is supposed to be,
            // we can't resume, so delete what we already have if something is on disk already.
            if (!downloadData.IsProgressKnown && File.Exists(downloadTo))
            {
                File.Delete(downloadTo);
            }

            if (downloadData.IsProgressKnown && File.Exists(downloadTo))
            {
                // We only support resuming on http requests
                if (!(downloadData.Response is HttpWebResponse))
                {
                    File.Delete(downloadTo);
                }
                else
                {
                    // Try and start where the file on disk left off
                    downloadData.start = new FileInfo(downloadTo).Length;

                    // If we have a file that's bigger than what is online, then something
                    // strange happened. Delete it and start again.
                    if (downloadData.start > urlSize)
                    {
                        File.Delete(downloadTo);
                    }
                    else if (downloadData.start < urlSize)
                    {
                        // Try and resume by creating a new request with a new start position
                        downloadData.response.Close();
                        req = downloadData.GetRequest(url);
                        ((HttpWebRequest)req).AddRange((int)downloadData.start);
                        downloadData.response = req.GetResponse();

                        if (((HttpWebResponse)downloadData.Response).StatusCode != HttpStatusCode.PartialContent)
                        {
                            // They didn't support our resume request.
                            File.Delete(downloadTo);
                            downloadData.start = 0;
                        }
                    }
                }
            }

            return(downloadData);
        }