wx.uploadFile(OBJECT) 上传
将本地资源上传到开发者服务器。如页面通过 wx.chooseImage(图片)/wx.chooseVideo(视频) 等接口获取到一个本地资源的临时文件路径后,可通过此接口将本地资源上传到指定服务器。客户端发起一个 HTTPS POST 请求,其中 Content-Type
为 multipart/form-data
。
OBJECT参数说明:
success返回参数说明:
示例代码:
wx.chooseImage({ success: function(res) { var tempFilePaths = res.tempFilePaths wx.uploadFile({ url: 'http://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址 filePath: tempFilePaths[], name: 'file', formData:{ 'user': 'test' }, success: function(res){ var data = res.data //do something } }) } })
wx.downloadFile(OBJECT) 下载
下载文件资源到本地。客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。
OBJECT参数说明:
注:文件的临时路径,在小程序本次启动期间可以正常使用,如需持久保存,需在主动调用 wx.saveFile,在小程序下次启动时才能访问得到。
示例代码:
wx.downloadFile({ url: 'http://example.com/audio/123', //仅为示例,并非真实的资源 success: function(res) { wx.playVoice({ filePath: res.tempFilePath }) } })