zoukankan      html  css  js  c++  java
  • 显示下载进度条的下载文件代码

    VB.NET


    <!--StartFragment-->  Public Shared Sub DownFile(ByVal URL As String, ByVal Filename As String, ByVal Prog As ProgressBar)
        Dim Myrq As HttpWebRequest = HttpWebRequest.Create(URL)
        Dim myrp As HttpWebResponse = Myrq.GetResponse
        Dim totalBytes As Long = myrp.ContentLength
        Prog.Maximum = totalBytes
        Dim st As Stream = myrp.GetResponseStream
        Dim so As Stream = New FileStream(Filename, FileMode.Create)
        Dim totalDownloadedByte As Long = 0
        Dim by(1024) As Byte
        Dim osize As Integer = st.Read(by, 0, by.Length)
        While osize > 0
          totalDownloadedByte = osize + totalDownloadedByte
          Application.DoEvents()
          so.Write(by, 0, osize)
          Prog.Value = totalDownloadedByte
          osize = st.Read(by, 0, by.LongLength)
        End While
        so.Close()
        st.Close()
      End Sub 

    C#
    public static void DownFile( string URL, string Filename, ProgressBar Prog )
    {
      System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
      System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
      long totalBytes = myrp.ContentLength;
      Prog.Maximum = (int)totalBytes;
      System.IO.Stream st = myrp.GetResponseStream();
      System.IO.Stream so = new System.IO.FileStream(Filename, System.IO.FileMode.Create);
      long totalDownloadedByte = 0;
      byte[] by = new byte[1024];
      int osize = st.Read(by, 0, (int)by.Length);
      while (osize > 0)
      {
        totalDownloadedByte = osize + totalDownloadedByte;
        Application.DoEvents();
        so.Write(by, 0, osize);
        Prog.Value = (int)totalDownloadedByte;
        osize = st.Read(by, 0, (int)by.Length);
      }
      so.Close();
      st.Close();
                }
  • 相关阅读:
    Angular5的new feature
    Angular集成UEditor
    Angular集成admin-lte框架
    Angular TypeScript开发环境集成jQuery扩展插件
    如何在 Docker 容器中运行 Kali Linux 2.0
    信息系统安全等级保护基本要求
    Angular学习笔记
    Ubuntu16.04部署phantomjs的一个问题
    仿探探卡片滑动vue封装并发布到npm
    使用vscode,新建.vue文件,tab自动生成vue代码模板
  • 原文地址:https://www.cnblogs.com/nianshi/p/798395.html
Copyright © 2011-2022 走看看