.NET开发

本类阅读TOP10

·vs.net 2005中文版下载地址收藏
·NHibernate快速指南(翻译)
·【小技巧】一个判断session是否过期的小技巧
·通过Web Services上传和下载文件
·?dos下编译.net程序找不到csc.exe文件
·VB/ASP 调用 SQL Server 的存储过程
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
·Autodesk官方最新的.NET教程(一)(vb.net版)
·Duwamish深入剖析-结构篇

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
看到有人用 WebClient来下载, 发篇用 WebRequest 实现有进度下载的吧.

作者:未知 来源:月光软件站 加入时间:2005-5-13 月光软件站

 

记得以前刚用的时候,webclient确实看着挺简单,但是实现起来,小文件是一下就下载完了.

大文件要一直下载完毕才行.

后来找了找,用 WebRequest 结合 WebResponse 可以实现 有进度提示的,下载文件..
下面是代码..是从我一个软件中提取出来的.只取关键部分说明...
=====================================================================
     if(Downloading==false) //如果无文件正在下载
     { 
      TempDown=CurrentFileName;
      if(CurrentFileName==""){TempDown=b;}
      WhichDown=1;
      System.Threading.Thread ApcThread2=new System.Threading.Thread(new System.Threading.ThreadStart(DownFile));
      ApcThread2.Start();
   
     }
     else
     {
      MessageBox.Show("对不起,当前正在下载文件.","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
     }      
##################
大概说明下,如果当前没有文件正在下载,则启动一个新线程....下载文件.. 下面是 DownFile函数的代码....
简单的地方就不做注释了.
=============================================================================
  //下载块#####################################################################################################
  private void DownFile()
  {
   if(TempDown!="")
   {
   
    if(Downloading==false) //如果无文件下载
    {
     long fb;
     this.apc_status_1.Text="正在连接到 " + TempDown;
     Downloading=true;
     try
     {
      //====尝试URL有效性,以及初始化下载界面
      WebRequest myre=WebRequest.Create(TempDown);
      WebResponse mwrite=myre.GetResponse();
      fb=mwrite.ContentLength;
      this.apc_status_1.Text="连接成功..开始下载..";
      pbar.Value=0;
      pbar.Maximum=(int)fb;
      pbar.Visible=true;
      this.ApcList.Height=156;
      //====开始下载
      WebClient wc=new WebClient();
      SaveFileDialog sf=new SaveFileDialog();
      sf.Title="请选择文件存放的位置";
      filename=CurrentFileName;
      sf.FileName=filename.Substring(filename.LastIndexOf("/")+1,filename.Length-filename.LastIndexOf("/")-1);
      sf.ShowDialog(this);
      filename=sf.FileName;
      if(filename!="")
      {
       Stream srm=wc.OpenRead(TempDown);
       StreamReader srmer=new StreamReader(srm);
       byte[] mbyte=new byte[fb];
       int allbyte=(int)mbyte.Length;
       int startbyte=0;
       while(fb>0)  //################   循环读取文件,并显示进度.....
       {
        Application.DoEvents();
        int m=srm.Read(mbyte,startbyte,allbyte);
        if(m==0){break;}
        startbyte+=m;
        allbyte-=m;
        pbar.Value+=m;
        int a1=(int)startbyte/1024;
        int a2=(int)fb/1024;
        this.apc_status_1.Text="连接成功..开始下载.." + a1.ToString() + "/" + a2.ToString() + "KB";//startbyte + "/" + fb.ToString();
       }

       FileStream fs = new FileStream(filename,FileMode.OpenOrCreate);
       fs.Write(mbyte,0,mbyte.Length);
       fs.Flush();


       srm.Close();
       srmer.Close();
       fs.Close();

       this.ApcList.Height=170;
       pbar.Visible=false;
       this.apc_status_1.Text="文件下载完毕!";
      }                     


     }
     catch(WebException exp) //如地址无效或者找不到文件
     {
      MessageBox.Show(exp.Message,"听啪啪 提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
     }
     Downloading=false;
    }
    else
    {
     MessageBox.Show("对不起,当前正在下载文件.","听啪啪 提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
   }
   else
   {
    if(WhichDown==1)
    {
     MessageBox.Show("当前并无文件播放.","听啪啪 提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
    else
    {
     MessageBox.Show("请在列表中选择好想要下载的文件.","听啪啪 提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
   }
   
  }//下载块#####################################################################################################




相关文章

相关软件




月光软件程序下载编程文档电脑教程网站设计网址导航网络文学游戏天地幽默笑话生活休闲写作范文安妮宝贝
电脑技术编程开发网络专区谈天说地情感世界游戏元素分类游戏热门游戏体育运动手机专区业余爱好影视沙龙
音乐天地数码广场教育园地科学大观古今纵横谈股论金人文艺术医学保健动漫图酷二手专区地方风情各行各业

月光软件站·版权所有