# wx.cloud.uploadFile

将本地资源上传至云存储空间，如果上传至同一路径则是覆盖写

请求参数

| 字段 | 说明 | 数据类型 | 默认值 | 必填 |
| --  | -- | -- | -- | --
| cloudPath   | 云存储路径，命名限制见[文件名命名限制](/miniprogram/dev/wxcloudservice/wxcloud/guide/storage/naming)          | String | - | Y |
| filePath   | 要上传文件资源的路径  | String | - | Y |
| config   | 配置 | Object | - | N |
| success | 成功回调 | | | |
| fail | 失败回调 | | | |
| complete | 结束回调 | | | |

**config 对象定义**

| 字段 | 说明 | 数据类型 |
| --  | --   | --     |
| env | 使用的环境 ID，填写后忽略 init 指定的环境 | String |

success 返回参数

| 字段 | 说明 | 数据类型 |
| --  | --   | --     |
| fileID | 文件 ID | String |
| statusCode | 服务器返回的 HTTP 状态码 | Number |
| errMsg | 错误信息，格式 uploadFile:ok | String |

fail 返回参数

| 字段 | 说明 | 数据类型 |
| --  | --   | --     |
| errCode | [错误码](/miniprogram/dev/wxcloudservice/wxcloud/reference/errcode) | Number |
| errMsg | 错误信息，格式 uploadFile:fail msg | String |

**返回值**

如果请求参数中带有 success/fail/complete 回调中的任一个，则会返回一个 [UploadTask](/miniprogram/dev/api/network/upload/UploadTask.md) 对象，通过 [UploadTask](/miniprogram/dev/api/network/upload/UploadTask.md) 对象可监听上传进度变化事件，以及取消上传任务。

### 使用示例

Callback 风格
```js
wx.cloud.uploadFile({
  cloudPath: 'example.png',
  filePath: '', // 文件路径
  success: res => {
    // get resource ID
    console.log(res.fileID)
  },
  fail: err => {
    // handle error
  }
})
```

Promise 风格
```js
wx.cloud.uploadFile({
  cloudPath: 'example.png',
  filePath: '', // 文件路径
}).then(res => {
  // get resource ID
  console.log(res.fileID)
}).catch(error => {
  // handle error
})
```
