主题:[讨论]寻找jzyray
jzyray兄:
你说的WebClient,我用了下,发现一个问题~
在我单击下载安钮的时候,没有任何反应,也不报错,
以下是代码:
private void btnDown_Click(object sender, EventArgs e)
{
WebClient bo = new WebClient();
bo.DownloadFile("e:\\data\\98.rar","98.rar");
WebClient he = new WebClient();
Stream strm = he.OpenRead("e:\\data\\98.rar");
}
我在网上还找到一篇下载的文章,我不知道我的URL和FileName是不是填错了,如果错了,因该怎么填呢?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Threading;
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void StartDownload()
{
Start.Enabled = false;
string URL = srcAddress.Text;
int n = URL.LastIndexOf('/');
string URLAddress = URL.Substring(0,n);
string fileName = URL.Substring(n+1,URL.Length-n-1);
string Dir = tarAddress.Text;
string Path = Dir+'\\'+fileName;
try
{
WebRequest myre=WebRequest.Create(URLAddress);
}
catch(WebException exp)
{
MessageBox.Show(exp.Message,"Error");
}
try
{
statusBar.Text = "开始下载文件...";
client.DownloadFile(URLAddress,fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
statusBar.Text = "正在接收数据...";
while(allmybyte>0)
{
int m = str.Read(mbyte,startmbyte,allmybyte);
if(m==0)
break;
startmbyte+=m;
allmybyte-=m;
}
FileStream fstr = new FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
str.Close();
fstr.Close();
statusBar.Text = "下载完毕!";
}
catch(WebException exp)
{
MessageBox.Show(exp.Message,"Error");
statusBar.Text = "";
}
Start.Enabled = true;
}
private void Start_Click(object sender, System.EventArgs e)
{
Thread th = new Thread(new ThreadStart(StartDownload));
th.Start();
}
}
}
你说的WebClient,我用了下,发现一个问题~
在我单击下载安钮的时候,没有任何反应,也不报错,
以下是代码:
private void btnDown_Click(object sender, EventArgs e)
{
WebClient bo = new WebClient();
bo.DownloadFile("e:\\data\\98.rar","98.rar");
WebClient he = new WebClient();
Stream strm = he.OpenRead("e:\\data\\98.rar");
}
我在网上还找到一篇下载的文章,我不知道我的URL和FileName是不是填错了,如果错了,因该怎么填呢?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Threading;
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void StartDownload()
{
Start.Enabled = false;
string URL = srcAddress.Text;
int n = URL.LastIndexOf('/');
string URLAddress = URL.Substring(0,n);
string fileName = URL.Substring(n+1,URL.Length-n-1);
string Dir = tarAddress.Text;
string Path = Dir+'\\'+fileName;
try
{
WebRequest myre=WebRequest.Create(URLAddress);
}
catch(WebException exp)
{
MessageBox.Show(exp.Message,"Error");
}
try
{
statusBar.Text = "开始下载文件...";
client.DownloadFile(URLAddress,fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
statusBar.Text = "正在接收数据...";
while(allmybyte>0)
{
int m = str.Read(mbyte,startmbyte,allmybyte);
if(m==0)
break;
startmbyte+=m;
allmybyte-=m;
}
FileStream fstr = new FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write);
fstr.Write(mbyte,0,startmbyte);
str.Close();
fstr.Close();
statusBar.Text = "下载完毕!";
}
catch(WebException exp)
{
MessageBox.Show(exp.Message,"Error");
statusBar.Text = "";
}
Start.Enabled = true;
}
private void Start_Click(object sender, System.EventArgs e)
{
Thread th = new Thread(new ThreadStart(StartDownload));
th.Start();
}
}
}