static void Main(string[] args) { FileDownloader downloade = new FileDownloader(); downloade.DownloadComplete += new EventHandler(downloade_DownloadedComplete); downloade.ProgressChanged += new DownloadProgressHandler(downloade_ProgressChanged); downloade.Download("http://gianfratti.com/media/downloads/ContratoServicos.doc", "c:\\"); }
public void executeOperation(Button btn, Label lbl, String url, IDownloadObserver observer) { mIDownloadObserver = observer; // if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { if (mIDownloadObserver != null) { mIDownloadObserver.onMessage(new DownloadArgument("NetworkFailed")); } return; } if (btn.Text == "Cancel") { btn.Text = "Download"; cancel(url); return; } FileDownloader fd = create(lbl, url, observer, btn); if (fd.DownloadStatus == "Continue") { return; } if (fd.DownloadStatus != "Completed") { fd.Download(url, this); btn.Text = "Cancel"; btn.Enabled = true; } }
public void AddNewDownload(String url) { if (currentTask == null) { currentTask = Task <byte[]> .Factory.StartNew(() => (fileDownloader.Download(url))); } else { currentTask = currentTask.ContinueWith( (previousTask) => { results.Add(previousTask.Id, previousTask.Result); return(fileDownloader.Download(url)); }); } }
static void Main(string[] args) { FileDownloader downloader = new FileDownloader(); downloader.DownloadComplete += new EventHandler(downloader_DownloadedComplete); downloader.ProgressChanged += new DownloadProgressHandler(downloader_ProgressChanged); downloader.Download("http://download.mozilla.org/?product=firefox-1.5.0.4&os=win&lang=en-US"); }
static void Main(string[] args) { FileDownloader downloader = new FileDownloader(); downloader.DownloadComplete += new EventHandler(downloader_DownloadedComplete); downloader.ProgressChanged += new DownloadProgressHandler(downloader_ProgressChanged); downloader.Download("https://download-installer.cdn.mozilla.net/pub/firefox/releases/77.0.1/win64/en-GB/Firefox%20Setup%2077.0.1.exe"); }
static void Main(string[] args) { FileDownloader downloade = new FileDownloader(); downloade.DownloadComplete += new EventHandler(downloade_DownloadedComplete); downloade.ProgressChanged += new DownloadProgressHandler(downloade_ProgressChanged); downloade.Download("http://gianfratti.com/media/downloads/ContratoServicos.doc", "c:\\"); }
public static DownloadTaskErrorCode GetByUrl( string url, string hostName, string dest, int timeout, EventHandler callbackComplete, DownloadProgressHandler callbackProgress, out string errMsg) { DownloadTaskErrorCode errCode = DownloadTaskErrorCode.Success; #if USE_CURL_DOWNLOADER FileDownloaderEx downloader = new FileDownloaderEx(); #else FileDownloader downloader = new FileDownloader(); #endif if (callbackComplete != null) { downloader.DownloadComplete += callbackComplete; } if (callbackProgress != null) { downloader.ProgressChanged += callbackProgress; } errMsg = ""; try { downloader.Download(url, hostName, dest, timeout); } catch (WebException webException) { errMsg = webException.Message; errCode = DownloadTaskErrorCode.NetworkError; } catch (ArgumentException argException) { errMsg = argException.Message; errCode = DownloadTaskErrorCode.UrlArgError; } catch (IOException ioException) { errMsg = ioException.Message; errCode = DownloadTaskErrorCode.IOError; } catch (Exception e) { errMsg = e.Message; errCode = DownloadTaskErrorCode.Unknown; } return(errCode); }
public FileDownloader exeute(Label lblProgress, String url, IDownloadObserver IDownloadObserver) { mIDownloadObserver = IDownloadObserver; // FileDownloader fd = new FileDownloader(lblProgress); this.Add(fd); fd.Download(url, this); return(fd); }
/// <summary> /// return false when no more task /// </summary> /// <returns></returns> private Boolean WorkOnce() { Task activeTask; DownloadTaskInfo activeTaskSnapshot; lock (m_lock) { if (m_stop) //stop { return(false); } if (m_taskQueue.Count == 0) //nothing to do { return(false); } activeTask = m_taskQueue[0]; m_taskQueue.RemoveAt(0); activeTaskSnapshot = activeTask.ToDownloadTaskInfo(); activeTask.status = DownloadTaskStatus.Downloading; } if (File.Exists(activeTaskSnapshot.localPath)) //local file exist, check whether already finished { #if USE_CURL_DOWNLOADER if (!FileDownloaderEx.IsFileInProgress(activeTaskSnapshot.localPath)) #else if (!FileDownloader.IsFileInProgress(activeTaskSnapshot.localPath)) #endif { Int64 fileSize; string md5string = CalcFileMd5AndSize(activeTaskSnapshot.localPath, out fileSize); if (CompareMd5(md5string, activeTaskSnapshot.md5) == 0) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Finished; activeTask.totalSize = fileSize; activeTask.finishedSize = fileSize; RaiseTaskEndEvent(activeTask.ToDownloadTaskInfo()); return(true); } } else //file content is not correct { File.Delete(activeTaskSnapshot.localPath); //download again } } } #if USE_CURL_DOWNLOADER FileDownloaderEx downloader = new FileDownloaderEx(); #else FileDownloader downloader = new FileDownloader(); #endif Boolean bCanceled = false; downloader.ProgressChanged += (sender, arg) => { //update task status lock (m_lock) { if (!activeTask.status.CanPause()) //stopped { downloader.Cancel(); bCanceled = true; } else { activeTask.totalSize = arg.TotalFileSize; activeTask.finishedSize = arg.CurrentFileSize; } } //Thread.Sleep(1); }; downloader.DownloadComplete += (sender, arg) => { }; try { Directory.CreateDirectory(m_rootDir); downloader.Download(activeTask.url, activeTask.hostName, activeTask.localPath, DownloadMan.reqTimeOut); if (!bCanceled) { Int64 fileSize; string md5string = CalcFileMd5AndSize(activeTaskSnapshot.localPath, out fileSize); if (CompareMd5(md5string, activeTaskSnapshot.md5) == 0) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Finished; activeTask.totalSize = fileSize; activeTask.finishedSize = fileSize; } } else //md5 dismatch { File.Delete(activeTaskSnapshot.localPath); lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.Md5Dismatch; activeTask.errorMessage = activeTask.errorCode.ToString(); } } } } catch (WebException e) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.NetworkError; activeTask.innerErrorCode = (Int32)e.Status; activeTask.errorMessage = e.Message; } } catch (IOException e) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.IOError; activeTask.innerErrorCode = 0; activeTask.errorMessage = e.Message; } } catch (ArgumentException e) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.UrlArgError; activeTask.innerErrorCode = 0; activeTask.errorMessage = e.Message; } } catch (Exception e) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.Unknown; activeTask.innerErrorCode = 0; activeTask.errorMessage = e.Message; } } if (!bCanceled) { RaiseTaskEndEvent(activeTask.ToDownloadTaskInfo()); } return(true); }
/// <summary> /// return false when no more task /// </summary> /// <returns></returns> private Boolean WorkOnce() { Task activeTask; DownloadTaskInfo activeTaskSnapshot; lock (m_lock) { if (m_taskQueue.Count == 0) //nothing to do return false; activeTask = m_taskQueue[0]; m_taskQueue.RemoveAt(0); activeTaskSnapshot = activeTask.ToDownloadTaskInfo(); activeTask.status = DownloadTaskStatus.Downloading; } if (File.Exists(activeTaskSnapshot.localPath)) //local file exist, check whether already finished { if (!FileDownloader.IsFileInProgress(activeTaskSnapshot.localPath)) { Int64 fileSize; if (CalcFileMd5AndSize(activeTaskSnapshot.localPath, out fileSize) == activeTaskSnapshot.md5) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Finished; activeTask.totalSize = fileSize; activeTask.finishedSize = fileSize; RaiseTaskEndEvent(activeTask.ToDownloadTaskInfo()); return true; } } else //file content is not correct { File.Delete(activeTaskSnapshot.localPath); //download again } } } FileDownloader downloader = new FileDownloader(); Boolean bCanceled = false; downloader.ProgressChanged += (sender, arg) => { //update task status lock (m_lock) { if (!activeTask.status.CanPause()) //stopped { downloader.Cancel(); bCanceled = true; } else { activeTask.totalSize = arg.TotalFileSize; activeTask.finishedSize = arg.CurrentFileSize; } } }; downloader.DownloadComplete += (sender, arg) => { }; try { Directory.CreateDirectory(m_rootDir); downloader.Download(activeTask.url, activeTask.localPath); if (!bCanceled) { Int64 fileSize; if (CalcFileMd5AndSize(activeTaskSnapshot.localPath, out fileSize) == activeTaskSnapshot.md5) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Finished; activeTask.totalSize = fileSize; activeTask.finishedSize = fileSize; } } else //md5 dismatch { File.Delete(activeTaskSnapshot.localPath); lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.Md5Dismatch; } } } } catch (WebException e) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.NetworkError; activeTask.innerErrorCode = (Int32)e.Status; activeTask.errorMessage = e.Message; } } catch (IOException e) { lock (m_lock) { activeTask.status = DownloadTaskStatus.Failed; activeTask.errorCode = DownloadTaskErrorCode.IOError; activeTask.innerErrorCode = 0; activeTask.errorMessage = e.Message; } } if (!bCanceled) RaiseTaskEndEvent(activeTask.ToDownloadTaskInfo()); return true; }