Webbrowser Control搜索目录以避免下载重复项 [英] Webbrowser Control search through directories to avoid downloading duplicates
问题描述
是否可以利用使用Web浏览器控件时触发的事件并从站点下载文件?即,如果我转到7zip网站并尝试在另存为"对话框中单击确定"时尝试下载该软件的副本,则希望能够检查 所选目录的副本已存在.我不是在询问我是否有处理的目录搜索部分,但我想利用该事件,用户已在其中选择了一个目录来保存文件.
Just PerdueIT
Is there a way to tap into the event that is triggered when using a web browser control and download a file from a site? i.e. If I go to the 7zip site and try to download a copy of the software when I hit ok on the save as dialog I want to be able to check the selected directory for a copy already existing. I'm not asking about the directory searching part of it that I have a handle on that but I want to tap into that event where the user has selected a directory to save the file in.
Just PerdueIT
推荐答案
Hi Just PerdueIT,
Hi Just PerdueIT,
如果要在WebBrowser控件中自定义文件下载过程,则可以注册导航事件并检查URL是否为文件下载URL.如果结果为是,则可以打开SaveFileDialog并执行所需的任何操作. 以下供您参考.
If you want to custom the process of file download in WebBrowser control, you could register Navigating event and check whether the URL is a file download URL. If the result is yes, you could open a SaveFileDialog and do anything what you want. Code below is for your reference.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigating += WebBrowser1_Navigating;
webBrowser1.Navigate("http://www.7-zip.org/download.html");
}
private void WebBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (IsDonwloadFileUrl(e.Url.Segments[e.Url.Segments.Length - 1]))
{
e.Cancel = true;
string filepath = null;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.FileName = e.Url.Segments[e.Url.Segments.Length - 1];
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
//You could write your code here to search or do other things
filepath = saveFileDialog1.FileName;
WebClient client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(e.Url, filepath);
}
}
}
public static bool IsDonwloadFileUrl(string url)
{
List<string> fileExtensions = new List<string> { ".exe", ".pdf", ".doc", ".docx", ".txt" };
foreach (var item in fileExtensions)
{
if (url.EndsWith(item))
{
return true;
}
}
return false;
}
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("File downloaded");
}
}
最好的问候,
王丽
Best Regards,
Li Wang
这篇关于Webbrowser Control搜索目录以避免下载重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!