将本地资源上传到开发者服务器。如页面通过 wx.chooseimage 等接口获取到一个本地资源的临时文件路径后,可通过此接口将本地资源上传到指定服务器。客户端发起一个https post请求,其中content-type
为multipart/form-data
。
object参数说明:
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
url | string | 是 | 开发者服务器url |
filepath | string | 是 | 要上传文件资源的路径 |
name | string | 是 | 文件对应的key , 开发者在服务器端通过这个key可以获取到文件二进制内容 |
header | object | 否 | http 请求 header,header中不能设置referer |
formdata | object | 否 | http 请求中其他额外的form data |
success | function | 否 | 接口调用成功的回调函数 |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
success返回参数说明:
参数 | 类型 | 说明 |
---|---|---|
data | string | 开发者服务器返回的数据 |
statuscode | number | http状态码 |
示例代码:
wx.chooseimage({
success:function(res){
var tempfilepaths = res.tempfilepaths
wx.uploadfile({
url: 'http://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
filepath: tempfilepaths[0],
name:"file",
formdata:{
"user":"test"
}
success: function(res){
var data = res.data
//do something
}
})
}
})
返回值:
基础库 1.4.0 开始支持,低版本需做兼容处理
返回一个uploadtask
对象,通过uploadtask
,可监听上传进度变化事件,以及取消上传任务。
uploadtask 对象的方法列表:
方法 | 参数 | 说明 | 最低版本 |
---|---|---|---|
onprogressupdate | callback | 监听上传进度变化 | 1.4.0 |
abort | 中断上传任务 | 1.4.0 |
onprogressupdate 返回参数说明:
参数 | 类型 | 说明 |
---|---|---|
progress | number | 上传进度百分比 |
totalbytessent | number | 已经上传的数据长度,单位 bytes |
totalbytesexpectedtosend | number | 预期需要上传的数据总长度,单位 bytes |
示例代码:
const uploadtask = wx.uploadfile({
url: 'http://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
filepath: tempfilepaths[0],
name: 'file',
formdata:{
'user': 'test'
},
success: function(res){
var data = res.data
//do something
}
})
uploadtask.onprogressupdate((res) => {
console.log('上传进度', res.progress)
console.log('已经上传的数据长度', res.totalbytessent)
console.log('预期需要上传的数据总长度', res.totalbytesexpectedtosend)
})
uploadtask.abort() // 取消上传任务
tip
: 最大并发限制是 10 个tip
: 默认超时时间和最大超时时间都是 60s下载文件资源到本地。客户端直接发起一个http get请求,返回文件的本地临时路径。
object参数说明:
参数 | 类型 | 必填 | 必填 |
---|---|---|---|
url | string | 是 | 下载资源的 url |
header | object | 否 | http 请求 header |
success | function | 否 | 下载成功后以 tempfilepath 的形式传给页面,res={tempfilepath:'文件的临时路径'} |
fail | function | 否 | 接口调用失败的回调函数 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
注:文件的临时路径,在小程序本次启动期间可以正常使用,如需持久保存,需在主动调用 wx.savefile,在小程序下次启动时才能访问得到。
示例代码:
wx.downloadfile({
url: 'http://example.com/audio/123', //仅为示例,并非真实的资源
success: function(res) {
wx.playvoice({
filepath: res.tempfilepath
})
}
})
返回值:
基础库 1.4.0 开始支持,低版本需做兼容处理
返回一个downloadtask
对象,通过downloadtask
,可监听下载进度变化事件,以及取消下载任务。
downloadtask 对象的方法列表:
方法 | 参数 | 说明 | 最低版本 |
---|---|---|---|
onprogressupdate | callback | 监听下载进度变化 | 1.4.0 |
abort | 中断下载任务 | 1.4.0 |
onprogressupdate 返回参数说明:
参数 | 类型 | 说明 |
---|---|---|
progress | number | 下载进度百分比 |
totalbyteswritten | number | 已经下载的数据长度,单位 bytes |
totalbytesexpectedtowrite | number | 预期需要下载的数据总长度,单位 bytes |
示例代码:
const downloadtask = wx.downloadfile({
url: 'http://example.com/audio/123', //仅为示例,并非真实的资源
success: function(res) {
wx.playvoice({
filepath: res.tempfilepath
})
}
})
downloadtask.onprogressupdate((res) => {
console.log('下载进度', res.progress)
console.log('已经下载的数据长度', res.totalbyteswritten)
console.log('预期需要下载的数据总长度', res.totalbytesexpectedtowrite)
})
downloadtask.abort() // 取消下载任务
tip
: 最大并发限制是 10 个tip
: 默认超时时间和最大超时时间都是 60stip
: 网络请求的 referer 是不可以设置的,格式固定为 https://servicewechat.com/{appid}/{version}/page-frame.html,其中{appid}
为小程序的 appid,{version}
为小程序的版本号,版本号为 0 表示为开发版。tip
: 6.5.3 以及之前版本的 ios 微信客户端header
设置无效