예제 #1
0
 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:\\");
 }
예제 #2
0
		/// <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;
		}