Send files
Attach files to text messages to share information (support chats) or improve collaboration (social conversations).
You can attach multiple files to a single message using Unity Chat SDK. They can be of any format, but the size of each file must be at most 5 MB.
Files are uploaded to a message sequentially - the more files you attach, the longer it takes for the files to upload. To improve user experience in your chat app, you can implement a progress bar indicating to the user how many files have already been uploaded.
Requires File Sharing
To store files with PubNub, you must have File Sharing enabled for your app's keyset in the Admin Portal, with the selected storage region and the desired file retention period. Consider aligning the file retention period with the retention you set for Message Persistence to store messages and files for the same period of time.
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