Beispiel #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);
            }
        }
Beispiel #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();
                }
            }
        }
Beispiel #3
0
        public static DownloadDataEx Create(string url, string hostname, string destFileName, int timeout)
        {
            String lastModified = "";

            // This is what we will return
            DownloadDataEx downloadData = new DownloadDataEx();

            downloadData.downloadTo = destFileName;

            long urlSize = downloadData.GetFileSize(url, hostname, timeout, ref lastModified);

            downloadData.size = urlSize;

            String downloadTo = destFileName;

            FingerPrint fingerPrint = LoadFingerPrint(downloadTo);

            // 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 ||
                lastModified == "" || fingerPrint.timeStamp != lastModified ||
                fingerPrint.fileSize != urlSize)
            {
                DeleteDestFile(downloadTo);
            }

            SaveFingerPrint(downloadTo, new FingerPrint {
                timeStamp = lastModified, fileSize = urlSize
            });

            if (downloadData.IsProgressKnown && File.Exists(downloadTo))
            {
                DeleteDestFile(downloadTo);
                downloadData.start = 0;
            }

            downloadData.downloadToStream = File.Open(downloadTo, FileMode.CreateNew, FileAccess.Write);

            return(downloadData);
        }