---
name: "uploadFile"
mode: "kit"
versionRequirements:
  - { name: "BaseKit", version: "2.0.1" }
  - { name: "@ray-js/ray", version: "0.6.5" }
platform:
  - "iOS"
  - "Android"
async: true
title: "p2p.uploadFile - P2P Upload file"
---

## uploadFile

> [VERSION] BaseKit >= 2.0.1 | @ray-js/ray >= 0.6.5

> [PLATFORM] iOS, Android

> ⚡ **Supports Promise** — Returns a Promise when success / fail / complete callbacks are omitted.

### Description

Upload a local resource to the server. The client sends an HTTPS POST request with content-type multipart/form-data

### Parameters

| Property | Type | Required | Default | Since | Description |
| --- | --- | --- | --- | --- | --- |
| `url` | `string` | Yes | - | `2.0.1` | Developer server URL |
| `filePath` | `string` | Yes | - | `2.0.1` | Path of the file resource to upload (local path) |
| `name` | `string` | Yes | - | `2.0.1` | The key for the file; the server can use this key to obtain the file’s binary content |
| `header` | `Record<string, string>` | No | - | `2.0.1` | HTTP request headers; Referer must not be set in the headers |
| `formData` | `Record<string, string>` | No | - | `2.0.1` | Additional form data in the HTTP request |
| `timeout` | `number` | No | - | `2.0.1` | Timeout in milliseconds |
| `method` | `"POST" \| "PUT"` | No | `'POST'` | `3.23.0` | HTTP method for the upload request; defaults to POST |
| `complete` | `() => void` | No | - | - | Completion callback (executed on both success and failure) |
| `success` | `(params: Object) => void` ↓see below | No | - | - | Callback on successful API call |
| `fail` | `(params: Object) => void` ↓see below | No | - | - | Failure callback |

#### success callback parameters

| Property | Type | Since | Description |
| --- | --- | --- | --- |
| `data` | `string` | `3.2.6` | Data returned by the developer server |
| `statusCode` | `number` | `3.2.6` | HTTP status code returned by the developer server |

#### fail callback parameters

| Property | Type | Description |
| --- | --- | --- |
| `errorMsg` | `string` | Error message |
| `errorCode` | `string \| number` | Error code |
| `innerError` | `Object` | Error extension |

##### fail(params).innerError properties

| Property | Type | Description |
| --- | --- | --- |
| `errorCode` | `string \| number` | Error extension code |
| `errorMsg` | `string` | Error extension message |


### Valid Values

##### `method` valid values

| Value | Description |
| --- | --- |
| `"POST"` | HTTP POST request |
| `"PUT"` | HTTP PUT request |


### Examples

#### Demo

```tsx
import { downloadFile, uploadFile } from '@ray-js/ray'

// Upload a local file: download a file to get a local path, then use uploadFile to upload it to the server; returns an UploadTask
downloadFile({
  url: "https://airtake-public-data-1254153901.cos.ap-shanghai.myqcloud.com/ttttestfile/test_image.jpg",
  success: ({ tempFilePath }) => {
    const task = uploadFile({
      url: "https://httpbin.org/post",
      filePath: tempFilePath,
      name: "file",
      success: data => console.log("Upload completed, status code:", data.statusCode),
      fail: error => console.error("Upload failed:", JSON.stringify(error)),
    });
    console.log("UploadTask created");
  },
  fail: error => console.error(error),
});
```
