---
source_url: https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/messages/files
title: Send files
updated_at: 2026-06-08T11:39:17.060Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Send files

Attach files to messages to share information or improve collaboration.

Chat SDK supports multiple file attachments per message. Each file must be 5 MB or less. Files upload sequentially, so consider implementing a progress indicator for better UX.

:::warning Requires File Sharing
Enable [File Sharing](https://youtu.be/6hnhZ8t1_CU) in the [Admin Portal](https://admin.pubnub.com/) and configure storage region and retention. Align file retention with [Message Persistence](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/messages/history) retention.
:::

## Send files

Attach files to a [draft message](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/messages/drafts) and publish it using the [Send()](https://www.pubnub.com/docs/chat/unity-chat-sdk/build/features/messages/drafts#send-a-draft-message) method.

### Method signature

To add files, set the `Files` property (`List<ChatInputFile>`) on a [MessageDraft](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/message-draft) object before calling `Send()`.

`ChatInputFile` is a struct with the following properties:

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| Name | string | Optional |  | Name of the file, like `document.pdf`. |
| Type | string | Optional |  | [MIME](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) type of the file, like `application/pdf`. |
| Source | string | Optional |  | Path to the file on the local filesystem. |

### Sample code

Attach a file to a draft message and send it.

```csharp
using System.Collections.Generic;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;

// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
var getChannel = await chat.GetChannel("some_channel");
if (getChannel.Error)
{
    Debug.LogError($"Could not get channel! Error: {getChannel.Exception.Message}");
    return;
}
var channel = getChannel.Result;
        
//Creating a Message Draft to attach files
var messageDraft = channel.CreateMessageDraft();
messageDraft.InsertText(0, "some message");
messageDraft.Files = new List<ChatInputFile>()
{
    new ChatInputFile()
    {
        Name = "some_file.txt",
        //Same as above because assuming it's in the same directory as the script
        Source = "some_file.txt",
        Type = "text"
    }
};
var sendResult = await messageDraft.Send();
        
//Checking sendResult in case there was an issue with the file e.g. it didn't exist, was too large,
//or the keyset didn't have Files functionality enabled
if (sendResult.Error)
{
    Debug.LogError($"Error when sending message with file: {sendResult.Exception.Message}");
}
```

## Get all message files

You can access the `Files` property of the [Message object](https://www.pubnub.com/docs/chat/unity-chat-sdk/learn/chat-entities/message) to list all files attached to a single message.

### Property signature

This property has the following signature:

```csharp
message.Files
```

| Property | Description |
| --- | --- |
| `Files`Type: `List<ChatFile>` | List of files attached to the message. |

`ChatFile` contains the following properties:

| Property | Description |
| --- | --- |
| `Name`Type: `string` | Name of the file, like `error-1.jpg`. |
| `Id`Type: `string` | Unique identifier assigned to the file by PubNub, like `736499374`. |
| `Url`Type: `string` | File's direct downloadable URL, like `https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg`. |
| `Type`Type: `string` | [MIME](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) type of the file, like `image/jpeg`. |

### Sample code

List all files attached to a message.

```csharp
using System.Collections.Generic;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;

// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
        
//Also works with messages from OnMessageReceived callback
var message = await chat.GetMessage("some_channel", "12345678912345678");
if (message.Error)
{
    Debug.LogError($"Could not get message! Error: {message.Exception.Message}");
    return;
}

var files = message.Result.Files;
foreach (var chatFile in files)
{
    Debug.Log($"Message file with ID: {chatFile.Id}, Name: {chatFile.Name}, URL: {chatFile.Url}, and Type: {chatFile.Type}");
}
```

## Get all channel files

`GetFiles()` returns all files attached to messages on a given channel.

### Method signature

This method takes the following parameters:

```csharp
channel.GetFiles(
    int limit = 0,
    string next = ""
)
```

#### Input

| Parameter | Description |
| --- | --- |
| `limit`Type: `int`Default: `0` | Number of files to return. When set to `0`, uses the default server value. |
| `next`Type: `string`Default: `""` | String token to get the next batch of files. |

#### Output

| Type | Description |
| --- | --- |
| `Task<ChatOperationResult<ChatFilesResult>>` | Returned object containing these fields: `Files`, `Next`, and `Total`. |

`ChatFilesResult` contains the following properties:

| Property | Description |
| --- | --- |
| `Files`Type: `List<ChatFile>` | List containing file details. |
| `Next`Type: `string` | Random string returned from the server, indicating a specific position in a data set. Used for forward pagination, it fetches the next page, allowing you to continue from where you left off. |
| `Total`Type: `int` | Total number of files. |

`ChatFile` contains the following properties:

| Property | Description |
| --- | --- |
| `Name`Type: `string` | Name of the file, like `error-1.jpg`. |
| `Id`Type: `string` | Unique identifier assigned to the file by PubNub, like `736499374`. |
| `Url`Type: `string` | File's direct downloadable URL, like `https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg`. |
| `Type`Type: `string` | [MIME](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) type of the file, like `image/jpeg`. |

### Sample code

List all files published on a channel.

```csharp
using System.Collections.Generic;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;

// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
var getChannel = await chat.GetChannel("some_channel");
if (getChannel.Error)
{
    Debug.LogError($"Could not get channel! Error: {getChannel.Exception.Message}");
    return;
}
var channel = getChannel.Result;
        
var files = await channel.GetFiles();
if (files.Error)
{
    Debug.LogError($"Error when trying to get files: {files.Exception.Message}");
    return;
}

foreach (var file in files.Result.Files)
{
    Debug.Log($"File ID: {file.Id}, file Name: {file.Name}, file URL: {file.Url}");
}
```

## Delete files

Delete sent files or files from published messages with the `DeleteFile()` method.

:::note Deletion consequences
Once you delete the file, you can't track which historical messages contained the deleted file to remove info about the file from those messages.
:::

### Method signature

This method takes the following parameters:

```csharp
channel.DeleteFile(
    string id,
    string name
)
```

#### Input

| Parameter | Description |
| --- | --- |
| `id` *Type: `string`Default: n/a | Unique identifier assigned to the file by PubNub. |
| `name` *Type: `string`Default: n/a | Name of the file. |

#### Output

| Type | Description |
| --- | --- |
| `Task<ChatOperationResult>` | Operation result indicating the success or failure of the deletion. |

### Sample code

Remove a file from a channel.

```csharp
using System.Collections.Generic;
using System.Threading.Tasks;
using PubnubApi;
using PubnubChatApi;
using UnityEngine;

// Configuration
PubnubChatConfig chatConfig = new PubnubChatConfig();
        
PNConfiguration pnConfiguration = new PNConfiguration(new UserId("myUniqueUserId"))
{
    SubscribeKey = "demo",
    PublishKey = "demo",
    Secure = true
};

// Initialize Unity Chat
var chatResult = await UnityChat.CreateInstance(chatConfig, pnConfiguration);
if (!chatResult.Error)
{
    chat = chatResult.Result;
}
var getChannel = await chat.GetChannel("some_channel");
if (getChannel.Error)
{
    Debug.LogError($"Could not get channel! Error: {getChannel.Exception.Message}");
    return;
}
var channel = getChannel.Result;

//ID and Name should be from either Message.Files or channel.GetFiles()
var delete = await channel.DeleteFile("file_id", "file_name");
```