💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# UploadTask ### UploadTask > 支持版本 >= 1.4.0 一个可以监听上传进度变化事件,以及取消上传任务的对象 #### 方法 ##### UploadTask.abort() 中断上传任务 ##### UploadTask.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"> uploadTask </span><span class="pun">=</span><span class="pln"> wx</span><span class="pun">.</span><span class="pln">uploadFile</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.weixin.qq.com/upload'</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"> filePath</span><span class="pun">:</span><span class="pln"> tempFilePaths</span><span class="pun">[</span><span class="lit">0</span><span class="pun">],</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> name</span><span class="pun">:</span><span class="pln"> </span><span class="str">'file'</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> formData</span><span class="pun">:{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="str">'user'</span><span class="pun">:</span><span class="pln"> </span><span class="str">'test'</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="pln"> success </span><span class="pun">(</span><span class="pln">res</span><span class="pun">){</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">const</span><span class="pln"> data </span><span class="pun">=</span><span class="pln"> res</span><span class="pun">.</span><span class="pln">data</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="com">//do something</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">uploadTask</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">totalBytesSent</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">totalBytesExpectedToSend</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">uploadTask</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/upload/UploadTask.html>