首页 > 开发 > 综合 > 正文

用C# Builder实现文件下载

2024-07-21 02:26:21
字体:
来源:转载
供稿:网友

一.概述:   
    本文通过一个实例向大家介绍用c# builder进行internet通讯编程的一些基本知识。我们知道.net类包含了请求/响应层、应用协议层、传输层等层次。在本程序中,我们运用了位于请求/响应层的webrequest类以及webclient类等来实现高抽象程度的internet通讯服务。本程序的功能是完成文件的下载。
二.实现原理:   
    程序实现的原理比较简单,主要用到了webclient类和filestream类。其中webclient类处于system.net名字空间中,该类的主要功能是提供向uri标识的资源发送数据和从uri标识的资源接收数据的公共方法。我们利用其中的downfile()方法将文件下载到本地。然后用filestream类的实例对象以数据流的方式将文件数据写入本地文件。这样就完成了文件的下载。
三.实现步骤:
    1.首先,打开c# builder,file->new->c# application,name这里我们设为"download"。
    2.主界界的设置。text设为“文件下载”,startposition设为centerscreen,maximizebox设为false,我们在主窗体上添加如下控件:两个标签控件label1,label2、一个文本框控件textbox1、一个按钮控件button1以及一个进度条控件progressbar1。
    label1:text为url;label2:text为下载进度;textbox1:text设为空;button1:text设为下载;
    3.程序的编码
     //过程downfile,用于完成文件的下载
  private void downfile()
  {
   string filename;
   webclient downfile=new webclient();
   long fbytes;
   if (textbox1.text!="")
   {
     savefiledialog1.showdialog();
     filename=savefiledialog1.filename;
     if(filename!= "")
     {
      //取得文件大小
      webrequest wr_request=webrequest.create(textbox1.text);
      webresponse wr_response=wr_request.getresponse();
      fbytes=wr_response.contentlength;
      progressbar1.maximum=(int)fbytes;
      progressbar1.step=1;
      wr_response.close();
                           //开始下载数据
      downfile.downloaddata(textbox1.text);
      stream strm = downfile.openread(textbox1.text);
      streamreader reader = new streamreader(strm);
      byte[] mbyte = new byte[fbytes];
      int allmybyte = (int)mbyte.length;
      int startmbyte = 0;
      while(fbytes>0)
      {
     int m = strm.read(mbyte,startmbyte,allmybyte);
     if(m==0) break;
     startmbyte+=m;
     allmybyte-=m;
     progressbar1.value+=m;
      }
      filestream fstrm = new filestream(filename,filemode.openorcreate,fileaccess.write);
      fstrm.write(mbyte,0,startmbyte);
      strm.close();
      fstrm.close();
      progressbar1.value=progressbar1.maximum;
     }
   } else
   {
      messagebox.show("没有输入要下载的文件!");
   }
  }

     //双击“下载”按钮,输入以下代码:
     thread th = new thread(new threadstart(downfile));
     th.start();

      //完整的代码如下:
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 download
{
 /// <summary>
 /// summary description for winform.
 /// </summary>
 public class winform : system.windows.forms.form
 {
  /// <summary>
  /// required designer variable.
  /// </summary>
  private system.componentmodel.container components = null;
  private system.windows.forms.label label1;
  private system.windows.forms.textbox textbox1;
  private system.windows.forms.button button1;
  private system.windows.forms.savefiledialog savefiledialog1;
  private system.windows.forms.label label2;
  private system.windows.forms.progressbar progressbar1;

  public winform()
  {
   //
   // required for windows form designer support
   //
   initializecomponent();

   //
   // todo: add any constructor code after initializecomponent call
   //
  }

  /// <summary>
  /// clean up any resources being used.
  /// </summary>
  protected override void dispose (bool disposing)
  {
   if (disposing)
   {
    if (components != null)
    {
     components.dispose();
    }
   }
   base.dispose(disposing);
  }

  #region windows form designer generated code
  /// <summary>
  /// required method for designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void initializecomponent()
  {
   this.label1 = new system.windows.forms.label();
   this.textbox1 = new system.windows.forms.textbox();
   this.button1 = new system.windows.forms.button();
   this.savefiledialog1 = new system.windows.forms.savefiledialog();
   this.label2 = new system.windows.forms.label();
   this.progressbar1 = new system.windows.forms.progressbar();
   this.suspendlayout();
   //
   // label1
   //
   this.label1.location = new system.drawing.point(40, 40);
   this.label1.name = "label1";
   this.label1.size = new system.drawing.size(40, 16);
   this.label1.tabindex = 0;
   this.label1.text = "url:";
   //
   // textbox1
   //
   this.textbox1.location = new system.drawing.point(72, 36);
   this.textbox1.name = "textbox1";
   this.textbox1.size = new system.drawing.size(256, 21);
   this.textbox1.tabindex = 1;
   this.textbox1.text = "";
   //
   // button1
   //
   this.button1.location = new system.drawing.point(256, 120);
   this.button1.name = "button1";
   this.button1.tabindex = 2;
   this.button1.text = "下载";
   this.button1.click += new system.eventhandler(this.button1_click);
   //
   // label2
   //
   this.label2.location = new system.drawing.point(8, 80);
   this.label2.name = "label2";
   this.label2.size = new system.drawing.size(72, 23);
   this.label2.tabindex = 3;
   this.label2.text = "下载进度:";
   //
   // progressbar1
   //
   this.progressbar1.location = new system.drawing.point(72, 80);
   this.progressbar1.name = "progressbar1";
   this.progressbar1.size = new system.drawing.size(256, 16);
   this.progressbar1.tabindex = 4;
   //
   // winform
   //
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(360, 173);
   this.controls.add(this.progressbar1);
   this.controls.add(this.label2);
   this.controls.add(this.button1);
   this.controls.add(this.textbox1);
   this.controls.add(this.label1);
   this.maximizebox = false;
   this.name = "winform";
   this.startposition = system.windows.forms.formstartposition.centerscreen;
   this.text = "文件下载";
   this.resumelayout(false);
  }
  #endregion

  /// <summary>
  /// the main entry point for the application.
  /// </summary>
  [stathread]
  static void main()
  {
   application.run(new winform());
  }

  private void downfile()
  {
   string filename;
   webclient downfile=new webclient();
   long fbytes;
   if (textbox1.text!="")
   {
     savefiledialog1.showdialog();
     filename=savefiledialog1.filename;
     if(filename!= "")
     {
      //取得文件大小
      webrequest wr_request=webrequest.create(textbox1.text);
      webresponse wr_response=wr_request.getresponse();
      fbytes=wr_response.contentlength;
      progressbar1.maximum=(int)fbytes;
      progressbar1.step=1;
      wr_response.close();
      //开始下载数据
      downfile.downloaddata(textbox1.text);
      stream strm = downfile.openread(textbox1.text);
      streamreader reader = new streamreader(strm);
      byte[] mbyte = new byte[fbytes];
      int allmybyte = (int)mbyte.length;
      int startmbyte = 0;
      while(fbytes>0)
      {
     int m = strm.read(mbyte,startmbyte,allmybyte);
     if(m==0) break;
     startmbyte+=m;
     allmybyte-=m;
     progressbar1.value+=m;
      }
      filestream fstrm = new filestream(filename,filemode.openorcreate,fileaccess.write);
      fstrm.write(mbyte,0,startmbyte);
      strm.close();
      fstrm.close();
      progressbar1.value=progressbar1.maximum;
     }
   } else
   {
      messagebox.show("没有输入要下载的文件!");
   }
  }
  private void button1_click(object sender, system.eventargs e)
  {
     thread th = new thread(new threadstart(downfile));
     th.start();
  }
 }
}

         4.按f9运行程序,输入一下载地址,点“下载”按钮试试。


四.结束语:  
    以上用一个简单的例子向大家展示了如何用c# builder实现文件的下载,我们不难发现用c# builder进行internet通讯编程是非常方便的。在上面的程序中,我们仅仅用到了webclient类的一些方法,而webclient类不只是提供了文件下载的方法,还提供了文件上传等方法,有兴趣的读者不妨自己试试,查查帮助。
      程序中使用了多线程,这是因为webclient类占有的资源校大,在下载文件会使整个窗口的显示不完整。如果不用多线程的话,下载文件的时候,你根本没法移动窗口或进行其它的一些操作。

注册会员,创建你的web开发资料库,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表