Beispiel #1
0
        /// <summary>
        /// Begin downloading the file at the specified url, and save it to the given folder.
        /// </summary>
        public void Download(string url, string destFolder)
        {
            DownloadData data = null;

            this.canceled = false;

            try
            {
                // get download details
                data = DownloadData.Create(url, destFolder, this.proxy);
                // Find out the name of the file that the web server gave us.
                string destFileName = Path.GetFileName(data.Response.ResponseUri.ToString());


                // The place we're downloading to (not from) must not be a URI,
                // because Path and File don't handle them...
                destFolder         = destFolder.Replace("file:///", "").Replace("file://", "");
                this.downloadingTo = Path.Combine(destFolder, destFileName);

                // Create the file on disk here, so even if we don't receive any data of the file
                // it's still on disk. This allows us to download 0-byte files.
                if (!File.Exists(downloadingTo))
                {
                    FileStream fs = File.Create(downloadingTo);
                    fs.Close();
                }

                // create the download buffer
                byte[] buffer = new byte[downloadBlockSize];

                int readCount;

                // update how many bytes have already been read
                long totalDownloaded = data.StartPoint;

                bool gotCanceled = false;


                // boucle de téléchargement
                while ((int)(readCount = data.DownloadStream.Read(buffer, 0, downloadBlockSize)) > 0)
                {
                    // break on cancel
                    if (canceled)
                    {
                        gotCanceled = true;
                        data.Close();
                        break;
                    }

                    // update total bytes read
                    totalDownloaded += readCount;

                    // save block to end of file
                    SaveToFile(buffer, readCount, this.downloadingTo);

                    // send progress info
                    if (data.IsProgressKnown)
                    {
                        RaiseProgressChanged(totalDownloaded, data.FileSize);
                    }

                    // break on cancel
                    if (canceled)
                    {
                        gotCanceled = true;
                        data.Close();
                        break;
                    }
                }

                if (!gotCanceled)
                {
                    OnDownloadComplete();
                }
            }
            catch (UriFormatException e)
            {
                throw new ArgumentException(
                          String.Format("Could not parse the URL \"{0}\" - it's either malformed or is an unknown protocol.", url), e);
            }
            finally
            {
                if (data != null)
                {
                    data.Close();
                }
            }
        }
Beispiel #2
0
        public void Download(string url, string destFolder, string filename = "")
        {
            DownloadData data = null;

            this.canceled = false;

            try
            {
                data = DownloadData.Create(url, destFolder, this.proxy);
                string destFileName = Path.GetFileName(data.Response.ResponseUri.ToString());

                destFolder = destFolder.Replace("file:///", "").Replace("file://", "");

                this.downloadingTo = (filename == "") ? Path.Combine(destFolder, destFileName) : Path.Combine(destFolder, filename);

                if (!File.Exists(downloadingTo))
                {
                    FileStream fs = File.Create(downloadingTo);
                    fs.Close();
                }

                byte[] buffer = new byte[downloadBlockSize];
                int    readCount;

                long totalDownloaded = data.StartPoint;
                bool gotCanceled     = false;

                while ((int)(readCount = data.DownloadStream.Read(buffer, 0, downloadBlockSize)) > 0)
                {
                    if (canceled)
                    {
                        gotCanceled = true;
                        data.Close();
                        break;
                    }

                    totalDownloaded += readCount;
                    SaveToFile(buffer, readCount, this.downloadingTo);

                    if (data.IsProgressKnown)
                    {
                        RaiseProgressChanged(totalDownloaded, data.FileSize);
                    }

                    if (canceled)
                    {
                        gotCanceled = true;
                        data.Close();
                        break;
                    }
                }

                if (!gotCanceled)
                {
                    OnDownloadComplete();
                }
            }
            catch (UriFormatException e)
            {
                throw new ArgumentException(
                          String.Format("Could not parse the URL \"{0}\" - it's either malformed or is an unknown protocol.", url), e);
            }
            finally
            {
                if (data != null)
                {
                    data.Close();
                }
            }
        }