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.
Requires File Sharing
Enable File Sharing in the Admin Portal and configure storage region and retention. Align file retention with Message Persistence retention.
Send files
You can attach files to a draft message and publish it using the Send() method, or send files directly using the SendText() method.
Method signature
To add files, you must add elements to the Files property (List<ChatInputFile>) of a MessageDraft object.
Alternatively, you can pass files directly to the SendText() method using the SendTextParams.Files property.
ChatInputFile is a struct with the following properties:
| Property | Description |
|---|---|
NameType: string | Name of the file, like document.pdf. |
TypeType: string | MIME type of the file, like application/pdf. |
SourceType: string | Path to the file on the local filesystem. |
Sample code
Attach a file to a text message using SendText().
1
Attach a file to a draft message using MessageDraft.Files.
1
Get all message files
You can access the Files property of the Message object to list all files attached to a single message.
Property signature
This property has the following signature:
1message.Files
| Property | Description |
|---|---|
FilesType: List<ChatFile> | List of files attached to the message. |
ChatFile contains the following properties:
| Property | Description |
|---|---|
NameType: string | Name of the file, like error-1.jpg. |
IdType: string | Unique identifier assigned to the file by PubNub, like 736499374. |
UrlType: string | File's direct downloadable URL, like https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg. |
TypeType: string | MIME type of the file, like image/jpeg. |
Sample code
List all files attached to a message.
1
Get all channel files
GetFiles() returns all files attached to messages on a given channel.
Method signature
This method takes the following parameters:
1channel.GetFiles(
2 int limit = 0,
3 string next = ""
4)
Input
| Parameter | Description |
|---|---|
limitType: intDefault: 0 | Number of files to return. When set to 0, uses the default server value. |
nextType: stringDefault: "" | 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 |
|---|---|
FilesType: List<ChatFile> | List containing file details. |
NextType: 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. |
TotalType: int | Total number of files. |
ChatFile contains the following properties:
| Property | Description |
|---|---|
NameType: string | Name of the file, like error-1.jpg. |
IdType: string | Unique identifier assigned to the file by PubNub, like 736499374. |
UrlType: string | File's direct downloadable URL, like https://ps.pndsn.com/v1/files/demo/channels/support/files/736499374/error-1.jpg. |
TypeType: string | MIME type of the file, like image/jpeg. |
Sample code
List all files published on a channel.
1
Delete files
Delete sent files or files from published messages with the DeleteFile() method.
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:
1channel.DeleteFile(
2 string id,
3 string name
4)
Input
| Parameter | Description |
|---|---|
id *Type: stringDefault: n/a | Unique identifier assigned to the file by PubNub. |
name *Type: stringDefault: 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.
1