企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# DownloadTask ### DownloadTask > 支持版本 >= 1.4.0 一个可以监听下载进度变化事件,以及取消下载任务的对象 #### 方法 ##### DownloadTask.abort() 中断下载任务 ##### DownloadTask.onProgressUpdate(function callback) 监听下载进度变化事件 #### 示例代码 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="kwd">const</span><span class="pln"> downloadTask </span><span class="pun">=</span><span class="pln"> wx</span><span class="pun">.</span><span class="pln">downloadFile</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> url</span><span class="pun">:</span><span class="pln"> </span><span class="str">'http://example.com/audio/123'</span><span class="pun">,</span><span class="pln"> </span><span class="com">//仅为示例,并非真实的资源</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> success </span><span class="pun">(</span><span class="pln">res</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> wx</span><span class="pun">.</span><span class="pln">playVoice</span><span class="pun">({</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> filePath</span><span class="pun">:</span><span class="pln"> res</span><span class="pun">.</span><span class="pln">tempFilePath</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">downloadTask</span><span class="pun">.</span><span class="pln">onProgressUpdate</span><span class="pun">((</span><span class="hljs-params"><span class="pln">res</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'下载进度'</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">.</span><span class="pln">progress</span><span class="pun">)</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'已经下载的数据长度'</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">.</span><span class="pln">totalBytesWritten</span><span class="pun">)</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'预期需要下载的数据总长度'</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">.</span><span class="pln">totalBytesExpectedToWrite</span><span class="pun">)</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">downloadTask</span><span class="pun">.</span><span class="pln">abort</span><span class="pun">()</span><span class="pln"> </span><span class="com">// 取消下载任务</span></code></li> </ol> ``` > 原文: <https://developers.weixin.qq.com/minigame/dev/api/network/download/DownloadTask.html>