// Stream DownLoad 直接下載檔案
using System.Net;
using mshtml;
using System.IO;
using System.Web;
using System.Threading;
public void strDown(string strUrl, string filename)
{
double maxsize = 0;
string url = strUrl;
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse HttpResponse = (HttpWebResponse)httpRequest.GetResponse();
maxsize = (int)HttpResponse.ContentLength;
System.IO.Stream Datastream = HttpResponse.GetResponseStream();
byte[] buffer = new byte[8192];
FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
int size = 0;
do
{
size = Datastream.Read(buffer, 0, buffer.Length);
if (size > 0)
{
fs.Write(buffer, 0, size);
}
} while (size > 0);
fs.Close();
HttpResponse.Close();
}
public void btnD1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(DownloadLink.Text);
loading();
HTMLDocument doc = webBrowser1.Document.Body.Document.DomDocument as HTMLDocument;
string receivePath = @"C:\Users\jwwu\Desktop\WebBrowser_Download\DownLoad\";
int CountA = 0;
foreach (IHTMLElement Element in doc.links)
{
string strUrl = Element.toString();
if (strUrl.Contains(".doc"))
{
CountA++;
strDown(strUrl, receivePath + System.IO.Path.GetFileName(strUrl));
}
}
if (CountA == 0)
{
MessageBox.Show("沒有word檔案可供下載", "下載檔案", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("總共" + CountA + "筆", "下載檔案", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
文章標籤
全站熱搜
