---
source_url: https://www.pubnub.com/docs/chat/swift-chat-sdk/build/features/messages/files
title: Send files
updated_at: 2026-06-16T12:48:53.839Z
---

> 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 (unlike the Swift SDK). 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/swift-chat-sdk/build/features/messages/history) retention.
:::

## Send files

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

### Method signature

To add files, you must add elements to the `files` field (`[InputFile]`) of a [MessageDraft](https://www.pubnub.com/docs/chat/swift-chat-sdk/learn/chat-entities/message-draft) object.

### Sample code

:::tip Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
:::

Attach two files of different formats to a text message.

```swift
// Assumes a "ChannelImpl" reference named "channel"
Task {
  let messageDraft = channel.createMessageDraft(isTypingIndicatorTriggered: channel.type != .public)
  messageDraft.update(text: "Here's some important documents:")
    
  let textFileStream = InputFile(
    name: "txtFileStream.txt",
    type: "text/plain",
    source: .stream(InputStream(fileAtPath: "filename.txt")!, contentType: "text/plain", contentLength: 14578)
  )
  let pdfFileStream = InputFile(
    name: "pdfFileStream.pdf",
    type: "application/pdf",
    source: .stream(InputStream(fileAtPath: "/path/to/document.pdf")!, contentType: "application/pdf", contentLength: 12578)
  )
    
  messageDraft.files.append(contentsOf: [textFileStream, pdfFileStream])
  try await messageDraft.send()
}
```

## Get all message files

`files` returns all files attached to a message.

### Method signature

This method has the following signature:

```swift
message.files
```

### Sample code

:::tip Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
:::

List all files attached to a message on the `support` channel.

```swift
// Assumes a "ChatImpl" reference named "chat"
Task {
  if let channel = try await chat.getChannel(channelId: "support") {
    if let message = try await channel.getMessage(timetoken: 16200000000000000) {
      if !message.files.isEmpty {
        for file in message.files {
          debugPrint("File Name: \(file.name), File Type: \(String(describing: file.type)), File URL: \(file.url)")
        }
      } else {
        debugPrint("The message does not contain any files.")
      }
    } else {
      debugPrint("Message does not exist")
    }
  }
}
```

## Get all channel files

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

### Method signature

This method takes the following parameters:

```swift
channel.getFiles(
    limit: Int = 100,
    next: String? = nil
  ) async throws -> (files: [GetFileItem], page: PubNubHashedPage?)
```

#### Input

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| limit | Int | Optional | `100` | Number of files to return. |
| next | String | Optional |  | String token to get the next batch of files. |

#### Output

| Parameter | Description |
| --- | --- |
| `(files: [GetFileItem], page: PubNubHashedPage?)`Type: `object` | Returned tuple containing these fields: `files` and `page`. |
| `> files`Type: `[GetFileItem]` | Array containing file details. |
| `> page`Type: `PubNubHashedPage` | Pagination 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. |

`GetFileItem` contains the following properties:

| Parameter | 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`. |

### Sample code

:::tip Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
:::

List all files published on the `support` channel.

```swift
// Assumes a "ChatImpl" reference named "chat"
Task {
  if let channel = try await chat.getChannel(channelId: "support") {
    let getFilesResult = try await channel.getFiles()
    debugPrint("Fetched files: \(getFilesResult.files)")
    debugPrint("Next page: \(String(describing: getFilesResult.page))")
      
    if let nextPage = getFilesResult.page, !getFilesResult.files.isEmpty {
      let resultsFromNextPage = try await channel.getFiles(next: nextPage.next)
    }
  }
}
```

## Delete files

`deleteFile()` removes files from PubNub storage.

:::note Endpoint limitation
After deleting a file, you cannot identify which historical messages contained it.
:::

### Method signature

This method takes the following parameters:

```swift
channel.deleteFile(
    id: String, 
    name: String
) async throws
```

#### 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

| Parameter | Description |
| --- | --- |
| `Void` | Indicates that the operation completed successfully with no additional data. |

### Sample code

:::tip Sample code
The code samples in Swift Chat SDK focus on asynchronous code execution.
You can also write synchronous code as the parameters are shared between the async and sync methods but we don't provide usage examples of such.
:::

Remove a file named `error-screenshot.png` from the `support` channel.

```swift
// Assumes a "ChatImpl" reference named "chat"
Task {
  if let channel = try await chat.getChannel(channelId: "support") {
    try await channel.deleteFile(
      id: "file-id-to-delete",
      name: "error-screenshot.png"
    )
  }
}
```