下载1. C#: /// <summary> /// 文件下载 /// </summary> /// <param name="FullFileName"></param> private void FileDownload(string FullFileName) { FileInfo DownloadFile = new FileInfo(FullFileName); Response.Clear(); Response.ClearHeaders(); Response.Buffer=false; Response.ContentType="application/octet-stream"; Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8)); Response.AppendHeader("Content-Length",DownloadFile.Length.ToString()); Response.WriteFile(DownloadFile.FullName); Response.Flush(); Response.End(); } 2. vb.net Public Sub WriteDLWindow(ByVal strFileName As String, ByVal page As System.Web.UI.Page) Try If File.Exists(page.MapPath(strFileName)) Then Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName)) '清除缓冲区流中的所有内容输出. page.Response.Clear() '向输出流添加HTTP头 [指定下载/保存 对话框的文件名] page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(TargetFile.Name))
'繁体格式 'page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8)) '向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度 page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString()) '表明输出的HTTP为流[stream],因此客户端只能下载. page.Response.ContentType = "application/octet-stream" '发送文件流到客户端. page.Response.WriteFile(TargetFile.FullName) '停止执行当前页 page.Response.End() End If Catch ex As Exception Throw ex End Try End Sub
自己改一下: private void DownLoadFile() { try { WebClient client = new WebClient(); if (!Directory.Exists(Application.StartupPath + @"\update")) { Directory.CreateDirectory(Application.StartupPath + @"\update"); } for (int i = 0; i <= this.listView1.Items.Count - 1; i++) { string fileName = this.listView1.Items[i].SubItems[0].Text; string url = @"http://172.16.120.155/update/UpdateFile/" + fileName; string file = Application.StartupPath + "\\update\\" + fileName; this.label2.Text = "正在下载:" + fileName; Stream myStream = client.OpenRead(url); byte[] bBuffer = new byte[10240]; int nRealCount = 0; DateTime dtStart=DateTime.Now ; TimeSpan ts; int lenth = Convert.ToInt32(this.listView1.Items[i].SubItems[1].Text.ToString()); int cur = 0; progressBar1.Value = 0; FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write); //do //{ // dtStart = DateTime.Now; // nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length); // if (nRealCount > 0) // { // fs.Write(bBuffer, 0, nRealCount); // } // ts = DateTime.Now - dtStart; // cur = cur + nRealCount; // double pe = cur; // pe = pe * 100 / lenth; // int va = Convert.ToInt32(pe); // progressBar1.Value = va; // this.label2.Text = "正在下载:" + fileName + " " + cur / 1024 + " KB/" + lenth / 1024 + " KB"; // MessageBox.Show("" + ts.Milliseconds + " " + ts.TotalMilliseconds); // // this.label2.Text = "正在下载:" + fileName + " " + cur / 1024 + " KB/" + lenth / 1024 + " KB"+" 下载速度:"+((double )nRealCount /(int)(ts.TotalMilliseconds/1000)) +" kb/s"; // // 计算流量可以用 nRealCount / ts.Seconds来进行估算 //} int value=0; int counter = 1; do { if (counter % 10 == 0) { ts = DateTime.Now - dtStart; int perSecondsValue = (int)((double)value / (ts.TotalMilliseconds / 1000)) / 1024; this.label3.Text = "速度:" +perSecondsValue +" k/s"; dtStart = DateTime.Now; value = 0; }
nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length); if (nRealCount > 0) { fs.Write(bBuffer, 0, nRealCount); } cur = cur + nRealCount; double pe = cur; pe = pe * 100 / lenth; int va = Convert.ToInt32(pe); progressBar1.Value = va;
/// <summary>
/// 文件下载
/// </summary>
/// <param name="FullFileName"></param>
private void FileDownload(string FullFileName)
{
FileInfo DownloadFile = new FileInfo(FullFileName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType="application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(DownloadFile.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",DownloadFile.Length.ToString());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
2. vb.net
Public Sub WriteDLWindow(ByVal strFileName As String, ByVal page As System.Web.UI.Page)
Try
If File.Exists(page.MapPath(strFileName)) Then
Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName))
'清除缓冲区流中的所有内容输出.
page.Response.Clear()
'向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(TargetFile.Name))
'繁体格式
'page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8)) '向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString())
'表明输出的HTTP为流[stream],因此客户端只能下载.
page.Response.ContentType = "application/octet-stream"
'发送文件流到客户端.
page.Response.WriteFile(TargetFile.FullName)
'停止执行当前页
page.Response.End()
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Response.ContentType = "application/ms-excel";
有没有WinForm的下载方法?
如果是ASP.NET,那么就简单了,直接Response.Redirect("http://文件的路径")就可以下载。
如果是WinForm程序,那就找个可以引用地址的控件,WebBrower?这个可以,只要
axWebBrowser1.Navigate("http://文件路径")
就可以开始下载,当然,仅仅为了引用一个地址而动用WebBrower好像有欠妥当,但我真的不知道有没有其他控件可以实现,实在想不到就这样用着吧。呵
谢谢!~
{
try
{
WebClient client = new WebClient(); if (!Directory.Exists(Application.StartupPath + @"\update"))
{
Directory.CreateDirectory(Application.StartupPath + @"\update");
}
for (int i = 0; i <= this.listView1.Items.Count - 1; i++)
{
string fileName = this.listView1.Items[i].SubItems[0].Text;
string url = @"http://172.16.120.155/update/UpdateFile/" + fileName;
string file = Application.StartupPath + "\\update\\" + fileName;
this.label2.Text = "正在下载:" + fileName; Stream myStream = client.OpenRead(url);
byte[] bBuffer = new byte[10240];
int nRealCount = 0;
DateTime dtStart=DateTime.Now ;
TimeSpan ts;
int lenth = Convert.ToInt32(this.listView1.Items[i].SubItems[1].Text.ToString());
int cur = 0;
progressBar1.Value = 0;
FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
//do
//{
// dtStart = DateTime.Now;
// nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);
// if (nRealCount > 0)
// {
// fs.Write(bBuffer, 0, nRealCount);
// }
// ts = DateTime.Now - dtStart;
// cur = cur + nRealCount;
// double pe = cur;
// pe = pe * 100 / lenth;
// int va = Convert.ToInt32(pe);
// progressBar1.Value = va;
// this.label2.Text = "正在下载:" + fileName + " " + cur / 1024 + " KB/" + lenth / 1024 + " KB";
// MessageBox.Show("" + ts.Milliseconds + " " + ts.TotalMilliseconds);
// // this.label2.Text = "正在下载:" + fileName + " " + cur / 1024 + " KB/" + lenth / 1024 + " KB"+" 下载速度:"+((double )nRealCount /(int)(ts.TotalMilliseconds/1000)) +" kb/s";
// // 计算流量可以用 nRealCount / ts.Seconds来进行估算
//}
int value=0;
int counter = 1;
do
{
if (counter % 10 == 0)
{
ts = DateTime.Now - dtStart; int perSecondsValue = (int)((double)value / (ts.TotalMilliseconds / 1000)) / 1024; this.label3.Text = "速度:" +perSecondsValue +" k/s";
dtStart = DateTime.Now;
value = 0;
}
nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);
if (nRealCount > 0)
{
fs.Write(bBuffer, 0, nRealCount);
}
cur = cur + nRealCount;
double pe = cur;
pe = pe * 100 / lenth;
int va = Convert.ToInt32(pe);
progressBar1.Value = va;
this.label2.Text = "正在下载:" + fileName + " " + cur / 1024 + " KB/" + lenth / 1024 + " KB";
value += nRealCount;
counter++;
}
while (nRealCount > 0);
fs.Close();
// client.DownloadFile(url, file);
this.listView1.Items[i].SubItems.Add("已下载");
} this.progressBar1.Visible = false;
this.label3.Visible = false;
this.label2.Text = "正在更新文件,请稍后...";
string[] fileDir = Directory.GetFileSystemEntries(Application.StartupPath + @"\update"); foreach (string str in fileDir)
{
int pos = str.LastIndexOf(@"\");
string FileName = str.Substring(pos);
string FilePath = Application.StartupPath + FileName;
File.Copy(str, FilePath, true);
} this.label2.Text = "程序更新已完成!"; this.label1.Text = this.finish;
this.btnNext.Enabled = false;
this.listView1.Visible = false;
this.btnExit.Text = "完成(&F)";
step++;
}
catch (Exception errMsg)
{
MessageBox.Show(errMsg.Message);
}
}
谢谢~
要是c/s我这里倒是有一个读写文件的类