File Sharing API for C# SDK
Allows users to upload and share files. You can upload any file of up to 5 MB in size. This feature is commonly used in social apps to share images, or in medical apps to share medical records for patients.
When a file is uploaded on a channel
, it's stored and managed using a storage service, and associated with your key. Subscribers to that channel
receive a file event which contains a file ID
, filename
, and optional description
.
Request execution
Use try
/catch
when working with the C# SDK.
If a request has invalid parameters (for example, a missing required field), the SDK throws an exception. If the request reaches the server but fails (server error or network issue), the error details are available in the returned status
.
try
{
PNResult<PNPublishResult> publishResponse = await pubnub.Publish()
.Message("Why do Java developers wear glasses? Because they can't C#.")
.Channel("my_channel")
.ExecuteAsync();
PNStatus status = publishResponse.Status;
Console.WriteLine("Server status code : " + status.StatusCode.ToString());
}
catch (Exception ex)
{
Console.WriteLine($"Request can't be executed due to error: {ex.Message}");
}
Send file
Upload the file to a specified channel.
This method covers the entire process of sending a file, including preparation, uploading the file to a cloud storage service, and post-uploading messaging on a channel.
For the last messaging step, SendFile
internally calls the PublishFileMessage
method to publish a message on the channel.
The published message contains metadata about the file, such as the file identifier and name, enabling others on the channel to find out about the file and access it.
Method(s)
pubnub.SendFile()
.Channel(string)
.File(string|byte[])
.FileName(string)
.Message(string)
.ShouldStore(bool)
.Meta(Dictionary<string, object>)
.Ttl(int)
.CustomMessageType(string)
Parameter | Description |
---|---|
Channel *Type: string | Channel for the file. |
File *Type: string or byte[] | Full path of the file with file name or an array of bytes. When using an array of bytes, you must set FileName . |
FileName Type: string | Name of the file to send. You can use it to override the default file name. |
Message Type: string | Message which should be sent along with file to specified channel . |
ShouldStore Type: bool | Whether PubNub published file message should be stored in channel history. |
Meta Type: Dictionary <string, object> | Dictionary<string, object> with values which should be used by PubNub service to filter file messages. |
TtL Type: int | How long message should be stored in channel's storage. |
CustomMessageType Type: string | A case-sensitive, alphanumeric string from 3 to 50 characters describing the business-specific label or category of the message. Dashes - and underscores _ are allowed. The value cannot start with special characters or the string pn_ or pn- . Examples: text , action , poll . |
Deprecated parameter
The CipherKey
parameter in this method is deprecated. We recommend that you configure the crypto module on your PubNub instance instead.
If you pass CipherKey
as an argument, it overrides the crypto module configuration and the legacy encryption with 128-bit cipher key entropy is used.
Sample code
Reference code
Response
{
"Timetoken":15957709330808500,
"FileId":"d9515cb7-48a7-41a4-9284-f4bf331bc770",
"FileName":"cat_picture.jpg"
}
Returns
The SendFile()
operation returns a PNResult``<PNFileUploadResult>
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Result | PNFileUploadResult | Returns a PNFileUploadResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNFileUploadResult
contains the following properties:
Property Name | Type | Description |
---|---|---|
Timetoken | long | Returns a PNFileUploadResult object. |
FileId | string | Returns the ID of the file. |
FileName | string | Returns the name of the file. |
List channel files
Retrieve list of files uploaded to Channel
.
Method(s)
pubnub.ListFiles()
.Channel(string)
Parameter | Description |
---|---|
Channel *Type: string Default: n/a | Channel to get list of files. |
Limit Type: int Default: 100 | Number of files to return. |
Next Type: string Default: n/a | 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. |
Sample code
Response
{
"FilesList":[
{
"Name":"cat_picture.jpg",
"Id":"d9515cb7-48a7-41a4-9284-f4bf331bc770",
"Size":25778,
"Created":"2020-07-26T13:42:06Z"
}],
"Count":1,
"Next":null
}
Returns
The ListFiles()
operation returns a PNResult<PNListFilesResult>
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Result | PNListFilesResult | Returns a PNListFilesResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNListFilesResult
contains the following properties:
Property Name | Type | Description |
---|---|---|
FilesList | list<PNFileResult> | List of channel files. |
Count | int | Number of files returned. |
Next | 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. |
PNFileResult
contains the following properties:
Property Name | Type | Description |
---|---|---|
Name | string | Name of the file. |
Id | string | ID of the file. |
Size | int | Size of the file. |
Created | string | Create date of the file. |