Giter Site home page Giter Site logo

httpclientdownloadwithprogressbar's Introduction

使用 HttpClient 進行檔案下載並顯示下載進度

透過 ProgressMessageHandler 類別的 HttpReceiveProgress 事件在取得 Http 服務資料時顯示目前的進度。

HttpReceiveProgress 事件提供的的 HttpProgressEventArgs 屬性如下:

名稱 備註
BytesTransferred 已取得位元數
TotalBytes 總資料位元數
ProgressPercentage 接收百分比

ProgressMessageHandler 類別位於 System.Net.Http.Formatting.Extension 套件中。命名空間為 System.Net.Http.Handlers,可透過 Nuget 進行取得。

範例程式碼說明

透過 HttpClient 取得 Ubuntu Server ISO 映像檔,並使用 ProgressMessageHandler 提供事件顯示下載檔案目前的進度。

範例程式碼

注意事項

  • 回應的 HttpResponse Header 需有 Content-Length 標頭,事件中的 TotalBytes 與 ProgressPercentage 才會有數值。
  • 預設 HttpClient 逾時時間為 100 秒,可透過 HttpClient.Timeout 屬性進行逾時時間變更。

參考資料

c# HttpClient上傳和下載的進度顯示問題

ProgressMessageHandler Class (System.Net.Http.Handlers)

httpclientdownloadwithprogressbar's People

Contributors

txstudio avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.