示例#1
0
        public static int OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
        {
            DownLoadParam    param      = (DownLoadParam)extraData;
            FileDownloaderEx downloader = param.downloader;
            DownloadDataEx   data       = param.downloadData;

            if (downloader.canceled)
            {
                return(-1);
            }

            int readCount = size * nmemb;

            param.totalDownloaded += readCount;

            // save block to end of file
            downloader.SaveToFile(buf, readCount, data.DownloadingToStream);

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

            if (downloader.canceled)
            {
                return(-1);
            }
            else
            {
                return(size * nmemb);
            }
        }
示例#2
0
        ///// <summary>
        ///// Begin downloading the file at the specified url, and save it to the current fileName.
        ///// </summary>
        //public void Download(string url)
        //{
        //    Download(url, "");
        //}
        /// <summary>
        /// Begin downloading the file at the specified url, and save it to the given fileName.
        /// </summary>
        public void Download(string url, string hostName, string destFileName, int timeout)
        {
            DownloadDataEx data = null;

            this.canceled = false;

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

                DownLoadParam param = new DownLoadParam()
                {
                    downloader      = this,
                    downloadData    = data,
                    totalDownloaded = data.StartPoint,
                };

                data.DoDownload(url, hostName, destFileName, timeout, downloadBlockSize, OnWriteData, param);

                bool gotCanceled = false;
                if (this.canceled)
                {
                    gotCanceled = true;
                }

                // stream could be incomplete
                if (data.IsProgressKnown)
                {
                    if (param.totalDownloaded < data.FileSize)
                    {
                        throw new WebException("date transfer not completed", WebExceptionStatus.ConnectionClosed);
                    }
                }

                if (!gotCanceled)
                {
                    RaiseProgressChanged(data.FileSize, data.FileSize);
                    data.CleanOnFinish();
                    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();
                }
            }
        }