合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# wx.uploadFile ### UploadTask wx.uploadFile(Object object) 将本地资源上传到开发者服务器,客户端发起一个 HTTPS POST 请求,其中 `content-type` 为 `multipart/form-data`。使用前请注意阅读[相关说明](https://developers.weixin.qq.com/minigame/dev/tutorial/ability/network.html)。 #### 参数 ##### Object object 属性类型默认值是否必填说明支持版本urlstring是开发者服务器地址filePathstring是要上传文件资源的路径namestring是文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容headerObject否HTTP 请求 Header,Header 中不能设置 RefererformDataObject否HTTP 请求中其他额外的 form datasuccessfunction否接口调用成功的回调函数failfunction否接口调用失败的回调函数completefunction否接口调用结束的回调函数(调用成功、失败都会执行)###### object.success 回调函数 **参数** **Object res** 属性类型说明支持版本datastring开发者服务器返回的数据statusCodenumber开发者服务器返回的 HTTP 状态码#### 返回值 ##### UploadTask > 支持版本 >= 1.4.0 一个可以监听上传进度进度变化的事件和取消上传的对象 #### 示例代码 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">wx</span><span class="pun">.</span><span class="pln">chooseImage</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><span class="pln"> </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"> tempFilePaths </span><span class="pun">=</span><span class="pln"> res</span><span class="pun">.</span><span class="pln">tempFilePaths</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> wx</span><span class="pun">.</span><span class="pln">uploadFile</span><span class="pun">({</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> url</span><span class="pun">:</span><span class="pln"> </span><span class="str">'https://example.weixin.qq.com/upload'</span><span class="pun">,</span><span class="pln"> </span><span class="com">//仅为示例,非真实的接口地址</span></code></li> <li class="l1"><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="l"><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="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> formData</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"> </span><span class="str">'user'</span><span class="pun">:</span><span class="pln"> </span><span class="str">'test'</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"> success </span><span class="pun">(</span><span class="pln">res</span><span class="pun">){</span></code></li> <li class="l1"><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="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="com">//do something</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="pln"> </span><span class="pun">}</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> </ol> ``` > 原文: <https://developers.weixin.qq.com/minigame/dev/api/network/upload/wx.uploadFile.html>