国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

看到有人用 WebClient來下載, 發篇用 WebRequest 實現有進度下載的吧.

2024-07-21 02:16:03
字體:
來源:轉載
供稿:網友
 
記得以前剛用的時候,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);
    }
   }
   
  }//下載塊#####################################################################################################

最大的網站源碼資源下載站,

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郧西县| 肥东县| 桑日县| 尚义县| 马公市| 五河县| 东阿县| 三江| 滁州市| 九龙坡区| 遵化市| 六安市| 柘荣县| 同仁县| 朝阳市| 健康| 贵南县| 兴山县| 宁河县| 涞水县| 犍为县| 南丰县| 静海县| 汤阴县| 大同市| 杭州市| 洞口县| 得荣县| 儋州市| 商都县| 阿拉善右旗| 钟山县| 平江县| 新绛县| 城固县| 绵竹市| 盐山县| 嘉荫县| 华坪县| 商都县| 东阿县|