● 将本地资源上传到服务器uploadFile
分类:上传
是否支持H5:不支持
参数
Object object
| 参数名 | 类型 | 默认值 | 必填 | 说明 |
|---|---|---|---|---|
| url | string | 是 | 开发者服务器接口地址 | |
| filePath | string/object/ArrayBuffer | 是 | 要上传文件资源的路径 (本地路径) | |
| name | string/object/ArrayBuffer | 是 | 文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容 | |
| header | Object | 否 | 设置请求的 header,header 中不能设置 Referer。content-type 默认为 application/json | |
| formData | Object | GET | 否 | HTTP 请求中其他额外的 form data |
| success | function | 否 | 接口调用成功的回调函数 | |
| fail | function | 否 | 接口调用失败的回调函数 | |
| complete | function | 否 | 接口调用结束的回调函数(接口调用成功、失败都会执行) |
object.success 回调函数
参数
Object res
| 属性 | 类型 | 说明 |
|---|---|---|
| data | string/Object/Arraybuffer | 开发者服务器返回的数据 |
| statusCode | number | 开发者服务器返回的 HTTP 状态码 |
调用示例
原生
let timestamp = new Date().getTime();
const _data = {
url: 'https://open-develop-stg1.paic.com.cn/upload',
filePath: '../../cd4345.jpg',
name: 'file',
header:{
'X-MINA-TIMESTAMP':timestamp
},
formData:{},
}
wx.uploadFile({
..._data,
success: function (res) {
console.log(res.data);
resolve(JSON.parse(res.data));
},
fail: function (res) {
console.log(e);
reject(e)
}
})