private void OnWebViewBeforeDownload(object sender, BeforeDownloadEventArgs e) { e.FilePath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", Path.GetFileName(e.FilePath)); }
/// <summary> /// Returns true to continue crawl of this url, else false /// </summary> /// <returns>True if this step should be cancelled, else false</returns> private bool OnBeforeDownload(CrawlStep crawlStep) { EventHandler <BeforeDownloadEventArgs> beforeDownloadTmp = BeforeDownload; if (beforeDownloadTmp.IsNull()) { return(crawlStep.IsAllowed); } BeforeDownloadEventArgs e = new BeforeDownloadEventArgs(!crawlStep.IsAllowed, crawlStep); beforeDownloadTmp(this, e); return(!e.Cancel); }
// 每个 dlItem 仅会调用一次;不一定在 OnDownloadUpdated 被调用前调用 private void OnBeforeDownload(BeforeDownloadEventArgs e, DownloadItem dlItem) { DownloadTask dlTask = TryGetDlTask(dlItem); }
private void OnExtensionWebViewBeforeDownload(Object sender, BeforeDownloadEventArgs e) { e.ShowDialog = false; e.FilePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(e.FilePath)); }
private void WebView_BeforeDownload(object sender, BeforeDownloadEventArgs e) { e.ShowDialog = false; string path = carpeta.Text + (object)Path.DirectorySeparatorChar +this.AnoSel+(object)Path.DirectorySeparatorChar+ this.MesSel + (object)Path.DirectorySeparatorChar+diaActual.ToString(); if (!Directory.Exists(path)) Directory.CreateDirectory(path); string tempPath = Path.GetTempPath(); e.FilePath = e.FilePath.Replace(tempPath, path + (object)Path.DirectorySeparatorChar); }
public async Task Crawl() { List <Task> downloadTask = new List <Task>(); pendingUrls.Enqueue(InitialUrl); while (pendingUrls.Count > 0) { if (!pendingUrls.TryDequeue(out var current)) { continue; } ; if (CompletedUrls.Count + DownloadingUrls.Count > MaxCount) { break; } //限制并发 if (MaxParallel > 0 && DownloadingUrls.Count >= MaxParallel) { await Task.Delay(100); continue; } //下载开始事件 var downEvent = new BeforeDownloadEventArgs() { Url = current, Cancelled = false }; BeforeDownload?.Invoke(this, downEvent); if (!downEvent.Cancelled) { lock (urlLock) { DownloadingUrls.Add(current); } var down = DownLoad(current, nameIndex++.ToString()) .ContinueWith(taks => { if (taks.IsFaulted) { FailedUrls.Add(current); lock (urlLock) { DownloadingUrls.Remove(current); } return; } CompletedUrls.Add(current); lock (urlLock) { DownloadingUrls.Remove(current); } var ev = new DownloadedEventArgs() { Html = taks.Result, OverrideUrlParse = false, Url = current }; Downloaded?.Invoke(this, ev); //覆盖默认的url解析 if (ev.OverrideUrlParse && ev.NextUrls != null) { foreach (var u in ev.NextUrls) { AddPending(u); } } else { Parse(taks.Result, current);//解析,并加入新的链接 } }); downloadTask.Add(down); Downloading?.Invoke(this, new DownloadingEventArgs() { Url = current }); } if (pendingUrls.Count <= 0) { await Task.WhenAny(downloadTask); downloadTask.RemoveAll(t => t.IsCompleted); } } await Task.WhenAll(downloadTask); this.CrawlerCompleted?.Invoke(this, new CrawlerCompletedEventArgs() { FailedCount = FailedUrls.Count, SuccessCount = CompletedUrls.Count }); }