示例#1
0
        /// <summary>
        /// Start to download.
        /// </summary>
        public void Download()
        {
            // Only idle download client can be started.
            if (this.Status != DownloadStatus.Initialized)
            {
                throw new ApplicationException(
                          "Only Initialized download client can be started.");
            }

            EnsurePropertyValid();

            // Set the status.
            Status = DownloadStatus.Downloading;

            if (!HasChecked)
            {
                CheckUrlAndFile(out string filename);
            }

            HttpDownloadClient client = new HttpDownloadClient(
                Url.AbsoluteUri,
                0,
                long.MaxValue,
                BufferSize,
                BufferCountPerNotification * BufferSize,
                BufferCountPerNotification);

            // Set the HasChecked flag, so the client will not check the URL again.
            client.TotalSize    = TotalSize;
            client.DownloadPath = DownloadPath;
            client.HasChecked   = true;
            client.Credentials  = Credentials;
            client.Proxy        = Proxy;

            // Register the events of HttpDownloadClients.
            client.DownloadProgressChanged += client_DownloadProgressChanged;
            client.StatusChanged           += client_StatusChanged;
            client.DownloadCompleted       += client_DownloadCompleted;

            downloadClients.Add(client);
            client.Download();
        }
示例#2
0
        /// <summary>
        /// Return S_OK (0) so that IE will stop to download the file itself.
        /// Else the default download user interface is used.
        /// </summary>
        public int Download(IMoniker pmk, IBindCtx pbc, uint dwBindVerb, int grfBINDF,
                            IntPtr pBindInfo, string pszHeaders, string pszRedir, uint uiCP)
        {
            // Get the display name of the pointer to an IMoniker interface that specifies the object to be downloaded.
            string name;

            pmk.GetDisplayName(pbc, null, out name);
            if (!string.IsNullOrEmpty(name))
            {
                Uri url;
                if (Uri.TryCreate(name, UriKind.Absolute, out url))
                {
                    Debug.WriteLine("DownloadManager: initial URL is: " + url);
                    NativeMethods.CreateBindCtx(0, out pbc);

                    downloadClient = new HttpDownloadClient(url.AbsoluteUri);
                    downloadClient.DownloadCompleted += (s, e) => { if (wb.DownloadCompleted != null)
                                                                    {
                                                                        wb.DownloadCompleted(s, e);
                                                                    }
                    };
                    downloadClient.DownloadProgressChanged += (s, e) => { if (wb.DownloadProgressChanged != null)
                                                                          {
                                                                              wb.DownloadProgressChanged(s, e);
                                                                          }
                    };
                    downloadClient.StatusChanged += (s, e) => { if (wb.DownloadStatusChanged != null)
                                                                {
                                                                    wb.DownloadStatusChanged(s, e);
                                                                }
                    };
                    downloadClient.OverWriteExsitFile = true;
                    if (this.GetCookie != null)
                    {
                        downloadClient.Cookie = this.GetCookie(url.AbsoluteUri);
                    }
                    string filename = string.Empty;
                    downloadClient.CheckUrl(out filename);
                    if (downloadClient.Status == HttpDownloadClientStatus.Completed)
                    {
                        return(0);
                    }
                    //if (string.IsNullOrEmpty(filename))
                    //{
                    //    downloadClient.DownloadPath = string.Format("{0}\\{1}",
                    //        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                    //        downloadClient.Url.Segments.Last());
                    //}
                    //else
                    //{
                    //    downloadClient.DownloadPath = string.Format("{0}\\{1}",
                    //        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                    //        filename);
                    //}
                    downloadClient.DownloadPath = Path.GetTempFileName();

                    downloadClient.Start();
                    //RegisterCallback(pbc, url);
                    //BindMonikerToStream(pmk, pbc);
                    return(0);
                }
            }
            return(1);
        }
示例#3
0
        void DownloadInternal(object obj)
        {
            if (Status != DownloadStatus.Waiting)
            {
                return;
            }

            try
            {
                EnsurePropertyValid();

                // Set the status.
                Status = DownloadStatus.Downloading;

                if (!HasChecked)
                {
                    CheckUrlAndFile(out string filename);
                }



                // If the file does not support "Accept-Ranges" header, then create one
                // HttpDownloadClient to download the file in a single thread, else create
                // multiple HttpDownloadClients to download the file.
                if (!IsRangeSupported)
                {
                    HttpDownloadClient client = new HttpDownloadClient(
                        Url.AbsoluteUri,
                        0,
                        long.MaxValue,
                        BufferSize,
                        BufferCountPerNotification * BufferSize,
                        BufferCountPerNotification)
                    {
                        // Set the HasChecked flag, so the client will not check the URL again.
                        TotalSize    = TotalSize,
                        DownloadPath = DownloadPath,
                        HasChecked   = true,
                        Credentials  = Credentials,
                        Proxy        = Proxy
                    };

                    downloadClients.Add(client);
                }
                else
                {
                    // Calculate the block size for each client to download.
                    int maxSizePerThread =
                        (int)Math.Ceiling((double)TotalSize / MaxThreadCount);
                    if (maxSizePerThread < MaxCacheSize)
                    {
                        maxSizePerThread = MaxCacheSize;
                    }

                    long leftSizeToDownload = TotalSize;

                    // The real threads count number is the min value of MaxThreadCount and
                    // TotalSize / MaxCacheSize.
                    int threadsCount =
                        (int)Math.Ceiling((double)TotalSize / maxSizePerThread);

                    for (int i = 0; i < threadsCount; i++)
                    {
                        long endPoint       = maxSizePerThread * (i + 1) - 1;
                        long sizeToDownload = maxSizePerThread;

                        if (endPoint > TotalSize)
                        {
                            endPoint       = TotalSize - 1;
                            sizeToDownload = endPoint - maxSizePerThread * i;
                        }

                        // Download a block of the whole file.
                        HttpDownloadClient client = new HttpDownloadClient(
                            Url.AbsoluteUri,
                            maxSizePerThread * i,
                            endPoint)
                        {
                            // Set the HasChecked flag, so the client will not check the URL again.
                            DownloadPath = DownloadPath,
                            HasChecked   = true,
                            TotalSize    = sizeToDownload,
                            Credentials  = Credentials,
                            Proxy        = Proxy
                        };
                        downloadClients.Add(client);
                    }
                }

                // Set the lastStartTime to calculate the used time.
                lastStartTime = DateTime.Now;

                // Start all HttpDownloadClients.
                foreach (var client in downloadClients)
                {
                    if (this.Proxy != null)
                    {
                        client.Proxy = Proxy;
                    }

                    // Register the events of HttpDownloadClients.
                    client.DownloadProgressChanged += client_DownloadProgressChanged;
                    client.StatusChanged           += client_StatusChanged;
                    client.DownloadCompleted       += client_DownloadCompleted;


                    client.BeginDownload();
                }
            }
            catch (Exception ex)
            {
                Cancel();
                OnDownloadCompleted(new DownloadCompletedEventArgs(
                                        null,
                                        DownloadedSize,
                                        TotalSize,
                                        TotalUsedTime,
                                        ex));
            }
        }