# Custom File Upload Handler

In certain circumstances, the Chat SDK needs to be able to upload files to an external server. Those situations are the following:

1. Uploading the user's profile image
2. Uploading thread images
3. Sending image messages

File upload is decoupled from the main project and is handled by the `UploadHandler` service.

In order to customize the upload behavior or upload files to a custom server, it's necessary to create a new upload handler service and then register it with the Chat SDK framework.

### The Upload Handler

To make a new upload handler you will need to subclass the `AbstractUploadHandler` for Android or the `BAbstractUploadHandler` for iOS.

The upload handler only has one method that needs to be implemented:

{% tabs %}
{% tab title="Java" %}

```java
Observable<FileUploadResult> uploadFile(byte[] data, String name, String mimeType);
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
-(RXPromise *) uploadFile:(NSData *)file withName: (NSString *) name mimeType: (NSString *) mimeType;
```

{% endtab %}
{% endtabs %}

#### iOS

To create a custom upload handler, you need to do the following:

1. Create a new subclass of the `BAbstractUploadHandler` class.
2. Implement the following method:

   ```
    -(RXPromise *) uploadFile:(NSData *)file withName: (NSString *) name mimeType: (NSString *) mimeType;
   ```
3. Register your new upload handler with the framework. In your App Delegate after you have called `BChatSDK.initialize` add the following line of code:

   ```
   BChatSDK.shared.networkAdapter.upload = [[YourUploadHandler alloc] init]
   ```

To see an example of how to implement the function, it's recommended to look at the `BFirebaseUploadHandler` class which demonstrates a model implementation for Google File Storage.

#### Android

To create a custom upload handler, you need to do the following:

1. Create a new subclass of the `AbstractUploadHandler` class.
2. Implement the following method:

   ```
   Observable<FileUploadResult> uploadFile(byte[] data,    String name, String mimeType);
   ```
3. Register your new upload handler with the framework. After you have called `ChatSDK.initialize` add the following line of code:

   ```
   ChatSDK.shared().a().upload = [[YourUploadHandler alloc] init]
   ```

To see an example of how to implement the function, it's recommended to look at the `FirebaseUploadHandler` class which demonstrates a model implementation for Google File Storage.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chat-sdk.gitbook.io/chat-sdk/guides/custom-file-upload-handler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
