您的位置:首页 > 移动开发 > 微信开发

微信小程序 网络API 上传、下载详解

2016-11-09 11:27 573 查看

微信小程序 网络API:

微信小程序 网络API发起请求

微信小程序 网络API 上传、下载

微信小程序 网络API Websocket

wx.uploadFile(OBJECT)

将本地资源上传到开发者服务器。如页面通过 wx.chooseImage 等接口获取到一个本地资源的临时文件路径后,可通过此接口将本地资源上传到指定服务器。客户端发起一个HTTPS POST请求,其中 Content-Type 为 multipart/form-data 。

OBJECT参数说明:

参数 类型 必填 说明
url String 开发者服务器url
filePath String 要上传文件资源的路径
name String 文件对应的key , 开发者在服务器端通过这个key可以获取到文件二进制内容
header Object HTTP 请求 Header
formData Object HTTP 请求中其他额外的form data
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码:

wx.chooseImage({
success:function(res){
var tempFilePaths = res.tempFilePaths;
wx.uploadFile({
url: 'http://example.com/upload',
filePath: tempFilePaths[0],
name:"file",
formData:{
"user":"test"
}
})
}
})

wx.downloadFile(OBJECT)

下载文件资源到本地。客户端直接发起一个HTTP GET请求,把下载到的资源根据 type 进行处理,并返回文件的本地临时路径。

OBJECT参数说明:

参数 类型 必填 必填
url String 下载资源的 url
type String 下载资源的类型,用于客户端识别处理,有效值:image/audio/video
header Object HTTP 请求 Header
success Function 下载成功后以 tempFilePath 的形式传给页面,res={tempFilePath:"文件的临时路径"}
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码:

wx.downloadFile({
url: 'http://example.com/audio/123',
type: 'audio',
success:function(res){
wx.playVoice({
filePath: res.tempFilePath
})
}
})

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息