CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

用webclient和stream下载文件的问题。

楼主51dianying()2004-12-03 22:34:09 在 .NET技术 / C# 提问

大家好,我昨天开始学习.net,看了几个示例程序,有如下一个问题请教:  
   
  程序员不说2话,翠花!上代码!  
  ---------------------------------------------  
  //就一个form  
  //textBox1   文本提示,textBox2   下载URL   textBox3   保存位置   btnUpdate   启动下载   btnExit   退出。  
  //完成功能:用户输入URL于textBox2中,输入保存位置   比如D:   在   textBox3   中  
  //点   下载   按钮,另开一个线程进行下载。  
  //问:如何实现边下载边保存?我开了一个100kb的缓冲区,但大于此数字的文件也只取这么多。  
  //本人水平非常有限,从下载按钮后执行的操作即可看出,那几个对话框是为了了解MessageBox的用法  
  //200分奖励。本程序为自己学习之用。  
  //以下是代码。  
   
  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;  
   
  namespace   Shiying.Inheritance  
  {  
  ///   <summary>  
  ///   Form1   的摘要说明。  
  ///   </summary>  
  public   class   Form1   :   System.Windows.Forms.Form  
  {  
  private   System.Windows.Forms.GroupBox   groupBox1;  
  private   System.Windows.Forms.TextBox   textBox1;  
  private   System.Windows.Forms.Button   btnExit;  
  private   System.Windows.Forms.Button   btnUpdate;  
  private   System.Windows.Forms.Label   label1;  
  private   System.Windows.Forms.TextBox   textBox2;  
  private   System.Windows.Forms.Label   label2;  
  private   System.Windows.Forms.TextBox   textBox3;  
  ///   <summary>  
  ///   必需的设计器变量。  
  ///   </summary>  
  private   System.ComponentModel.Container   components   =   null;  
   
  public   Form1()  
  {  
  //  
  //   Windows   窗体设计器支持所必需的  
  //  
  InitializeComponent();  
   
  //  
  //   TODO:   在   InitializeComponent   调用后添加任何构造函数代码  
  //  
  }  
   
  ///   <summary>  
  ///   清理所有正在使用的资源。  
  ///   </summary>  
  protected   override   void   Dispose(   bool   disposing   )  
  {  
  if(   disposing   )  
  {  
  if   (components   !=   null)    
  {  
  components.Dispose();  
  }  
  }  
  base.Dispose(   disposing   );  
  }  
   
  #region   Windows   窗体设计器生成的代码  
  ///   <summary>  
  ///   设计器支持所需的方法   -   不要使用代码编辑器修改  
  ///   此方法的内容。  
  ///   </summary>  
  private   void   InitializeComponent()  
  {  
  this.groupBox1   =   new   System.Windows.Forms.GroupBox();  
  this.textBox1   =   new   System.Windows.Forms.TextBox();  
  this.btnUpdate   =   new   System.Windows.Forms.Button();  
  this.btnExit   =   new   System.Windows.Forms.Button();  
  this.label1   =   new   System.Windows.Forms.Label();  
  this.textBox2   =   new   System.Windows.Forms.TextBox();  
  this.label2   =   new   System.Windows.Forms.Label();  
  this.textBox3   =   new   System.Windows.Forms.TextBox();  
  this.groupBox1.SuspendLayout();  
  this.SuspendLayout();  
  //    
  //   groupBox1  
  //    
  this.groupBox1.Controls.Add(this.textBox1);  
  this.groupBox1.Location   =   new   System.Drawing.Point(8,   8);  
  this.groupBox1.Name   =   "groupBox1";  
  this.groupBox1.Size   =   new   System.Drawing.Size(464,   232);  
  this.groupBox1.TabIndex   =   0;  
  this.groupBox1.TabStop   =   false;  
  this.groupBox1.Text   =   "系统提示:";  
  //    
  //   textBox1  
  //    
  this.textBox1.Location   =   new   System.Drawing.Point(16,   24);  
  this.textBox1.Multiline   =   true;  
  this.textBox1.Name   =   "textBox1";  
  this.textBox1.ReadOnly   =   true;  
  this.textBox1.Size   =   new   System.Drawing.Size(432,   192);  
  this.textBox1.TabIndex   =   0;  
  this.textBox1.Text   =   "textBox1";  
  //    
  //   btnUpdate  
  //    
  this.btnUpdate.Location   =   new   System.Drawing.Point(296,   328);  
  this.btnUpdate.Name   =   "btnUpdate";  
  this.btnUpdate.Size   =   new   System.Drawing.Size(80,   32);  
  this.btnUpdate.TabIndex   =   1;  
  this.btnUpdate.Text   =   "下载";  
  this.btnUpdate.Click   +=   new   System.EventHandler(this.button1_Click);  
  //    
  //   btnExit  
  //    
  this.btnExit.Location   =   new   System.Drawing.Point(392,   328);  
  this.btnExit.Name   =   "btnExit";  
  this.btnExit.Size   =   new   System.Drawing.Size(80,   32);  
  this.btnExit.TabIndex   =   2;  
  this.btnExit.Text   =   "退出";  
  this.btnExit.Click   +=   new   System.EventHandler(this.button2_Click);  
  //    
  //   label1  
  //    
  this.label1.Location   =   new   System.Drawing.Point(8,   264);  
  this.label1.Name   =   "label1";  
  this.label1.Size   =   new   System.Drawing.Size(80,   16);  
  this.label1.TabIndex   =   3;  
  this.label1.Text   =   "下载位置:";  
  //    
  //   textBox2  
  //    
  this.textBox2.Location   =   new   System.Drawing.Point(88,   264);  
  this.textBox2.Name   =   "textBox2";  
  this.textBox2.Size   =   new   System.Drawing.Size(384,   21);  
  this.textBox2.TabIndex   =   4;  
  this.textBox2.Text   =   "textBox2";  
  //    
  //   label2  
  //    
  this.label2.Location   =   new   System.Drawing.Point(8,   296);  
  this.label2.Name   =   "label2";  
  this.label2.Size   =   new   System.Drawing.Size(80,   16);  
  this.label2.TabIndex   =   5;  
  this.label2.Text   =   "保存位置:";  
  //    
  //   textBox3  
  //    
  this.textBox3.Location   =   new   System.Drawing.Point(88,   296);  
  this.textBox3.Name   =   "textBox3";  
  this.textBox3.Size   =   new   System.Drawing.Size(384,   21);  
  this.textBox3.TabIndex   =   6;  
  this.textBox3.Text   =   "textBox3";  
  //    
  //   Form1  
  //    
  this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);  
  this.ClientSize   =   new   System.Drawing.Size(480,   373);  
  this.Controls.Add(this.textBox3);  
  this.Controls.Add(this.label2);  
  this.Controls.Add(this.textBox2);  
  this.Controls.Add(this.label1);  
  this.Controls.Add(this.btnExit);  
  this.Controls.Add(this.btnUpdate);  
  this.Controls.Add(this.groupBox1);  
  this.Name   =   "Form1";  
  this.Text   =   "国研网在线更新系统.Net";  
  this.Load   +=   new   System.EventHandler(this.Form1_Load);  
  this.groupBox1.ResumeLayout(false);  
  this.ResumeLayout(false);  
   
  }  
  #endregion  
   
  ///   <summary>  
  ///   应用程序的主入口点。  
  ///   </summary>  
  [STAThread]  
  static   void   Main()    
  {  
  Application.Run(new   Form1());  
  }  
   
  private   void   button2_Click(object   sender,   System.EventArgs   e)  
  {  
  Application.Exit();  
  }  
   
  private   void   button1_Click(object   sender,   System.EventArgs   e)  
  {  
  //取对话框的值,Yes/No  
  System.Windows.Forms.DialogResult   rs;  
  rs=MessageBox.Show("LiveUpdate.Net开始更新","Liveupdate.net",MessageBoxButtons.YesNo);  
   
  if   (rs.ToString()=="Yes")  
  {  
  MessageBox.Show("you   click   yes!");  
  Thread   th   =   new   Thread(new   ThreadStart(DownloadFile));  
  th.Start();}  
  else   if   (rs.ToString()=="No")   MessageBox.Show   ("you   click   no!");  
  else  
  MessageBox.Show("Another   key   pressed!");  
   
  }  
   
  private   void   Form1_Load(object   sender,   System.EventArgs   e)  
  {  
   
  }  
  private   void   DownloadFile()  
  {  
  btnUpdate.Enabled   =   false;  
  string   URL   =   textBox2.Text   ;  
  //取文件名字,最后一个/之后的就是文件名字  
  int   n   =   URL.LastIndexOf('/');  
  string   URLAddress   =   URL.Substring(0,n);  
  string   fileName   =   URL.Substring(n+1,URL.Length-n-1);  
  string   Dir   =   textBox3.Text;  
  string   Path   =   Dir+'\\'+fileName;  
   
  try  
  {  
  WebRequest   myre=WebRequest.Create(URLAddress);  
  }  
  catch(WebException   exp)  
  {  
  MessageBox.Show(exp.Message,"Error");  
  }  
   
  try  
  {  
  System.Net.WebClient   client=new   WebClient();  
  textBox1.Text   =   "开始下载文件...";  
  client.DownloadFile(URL,fileName);  
  Stream   str   =   client.OpenRead(URL);  
  StreamReader   reader   =   new   StreamReader(str);  
  byte[]   mbyte   =   new   byte[100000];  
  int   allmybyte   =   (int)mbyte.Length;  
  int   startmbyte   =   0;  
  textBox1.Text   =   "正在接收数据...";  
  FileStream   fstr   =   new   FileStream(Path,FileMode.Append,FileAccess.Write);//Append只能和写一起用。  
  while(allmybyte>0)  
  {  
  int   m   =   str.Read(mbyte,startmbyte,allmybyte);  
  if(m==0)  
  break;  
  fstr.Write(mbyte,0,startmbyte);  
  startmbyte+=m;  
  allmybyte-=m;  
  }  
   
   
   
  str.Close();  
  fstr.Close();  
   
  textBox1.Text   =   "下载完毕!";    
  }  
  catch(WebException   exp)  
  {  
  MessageBox.Show(exp.Message,"Error");  
  textBox1.Text   =   "";  
  }  
   
  btnUpdate.Enabled   =   true;  
  }  
   
  }  
  }  
   
   
     
  问题点数:100、回复次数:6Top

1 楼51dianying()回复于 2004-12-03 22:35:37 得分 0

不好意思,没想到最多只能给100分。呵呵。Top

2 楼cnhgj(戏子) (没时间练太极)回复于 2004-12-03 23:07:36 得分 0

http://search.csdn.net/Expert/topic/1809/1809756.xml?temp=.5159571Top

3 楼OneDotRed(武装到眼神)回复于 2004-12-04 14:23:28 得分 100

因为1.x的WebClient类没有异步方法,使用WebClient无法实现你所说的,在我的Blog(http://blog.csdn.net/onedotred)中有篇文章,讲的是使用C#异步读取文件。  
   
  其方法是,先获取流的大小,然后按照一小段一小段读取,写至磁盘上。和你的这个问题相似。  
  对于你的这个问题,可以这样解决,使用HttpWebRequest和HttpWebResponse类,获取HttpWebResponse的ContentLength,HttpWebResponse.GetResponseStream()获得流,其他的就和异步读取文件一样了。    
   
  Top

4 楼MyXQ(重剑)回复于 2004-12-14 09:29:22 得分 0

markTop

5 楼tangxiaogang510(一起同行)回复于 2005-04-10 15:13:13 得分 0

upTop

6 楼tangxiaogang510(一起同行)回复于 2005-04-11 19:36:48 得分 0

如果是B/S的话,你这样只能下载到WEB本地电脑里面,却不能下载到客户端,Top

相关问题

  • 文件下载……
  • 下载文件
  • 文件下载
  • WebClient类下载文件报错,大侠进来看看啊!!!
  • 用WebClient下载文件,本地可以,服务器上不行
  • 下载文件 急!!!!!!!!!!!
  • php 文件下载
  • 求教!!!!!!!如何使用webclient或webrequest下载中文名的文件?
  • 救命:用WebClient下载文件.有后缀名为.ani的文件时报错(404 未找到)
  • 如何下载Excel文件?

关键词

  • .net
  • 文件
  • 下载
  • 代码
  • exp
  • btnupdate
  • btnexit
  • textbox
  • groupbox
  • webclient

得分解答快速导航

  • 帖主:51dianying
  • OneDotRed

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo