File Sharing API for PubNub 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
.
Send file
Upload file / data to specified channel
.
Method(s)
pubnub.SendFile()
.Channel(string)
.File(string|byte[])
.FileName(string)
.CipherKey(string)
.Message(string)
.ShouldStore(bool)
.Meta(Dictionary<string, object>)
.Ttl(int)
Parameter | Type | Required | Description |
---|---|---|---|
Channel | string | Yes | Channel for the file. |
File | string or byte[] | Yes | Full path of the file with file name or an array of bytes. When using an array of bytes, you must set FileName . |
FileName | string | Required when sending a file by byte[] | Name of the file to send. You can use it to override the default file name. |
CipherKey | string | Optional | Key to be used to encrypt uploaded data. If not provided, PNConfiguration CipherKey will be used, if provided. |
Message | string | Optional | Message which should be sent along with file to specified channel . |
ShouldStore | bool | Optional | Whether PubNub published file message should be stored in channel history. |
Meta | Dictionary<string, object> | Optional | Dictionary<string, object> with values which should be used by PubNub service to filter file messages. |
TtL | int | Optional | How long message should be stored in channel's storage. |
Basic Usage
PNResult<PNFileUploadResult> fileUploadResponse = await pubnub.SendFile()
.Channel("my_channel")
.File("cat_picture.jpg") //checks the bin folder if no path is provided
.CipherKey("my_cipher_key")
.Message("Look at this photo!")
.ExecuteAsync();
PNFileUploadResult fileUploadResult = fileUploadResponse.Result;
PNStatus fileUploadStatus = fileUploadResponse.Status;
if (!fileUploadStatus.Error && fileUploadResult != null)
{
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(fileUploadResult));
}
else
{
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(fileUploadStatus));
show all 16 linesResponse
{
"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 | Type | Required | Default | Description |
---|---|---|---|---|
Channel | string | Yes | Channel to get list of files. | |
Limit | int | Optional | 100 | Number of files to return. |
Next | string | Optional | Previously-returned cursor bookmark for fetching the next page. |
Basic Usage
PNResult<PNListFilesResult> listFilesResponse = await pubnub.ListFiles()
.Channel("my_channel")
.ExecuteAsync();
PNListFilesResult listFilesResult = listFilesResponse.Result;
PNStatus listFilesStatus = listFilesResponse.Status;
if (!listFilesStatus.Error && listFilesResult != null)
{
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(listFilesResult));
}
else
{
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(listFilesStatus));
}
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 | Cursor bookmark for fetching the next page. |
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. |
Get File Url
Generate URL which can be used to download file from target Channel
.
Method(s)
pubnub.GetFileUrl()
.Channel(string)
.FileId(string)
.FileName(string)
Parameter | Type | Required | Description |
---|---|---|---|
Channel | string | Yes | Name of channel within which file with name has been uploaded. |
FileId | string | Yes | Unique file identifier which has been assigned during file upload. |
FileName | string | Yes | Name under which uploaded file is stored for channel . |
Basic Usage
PNResult<PNFileUrlResult> getFileUrlResponse = await pubnub.GetFileUrl()
.Channel("my_channel")
.FileId("d9515cb7-48a7-41a4-9284-f4bf331bc770")
.FileName("cat_picture.jpg")
.ExecuteAsync();
PNFileUrlResult getFileUrlResult = getFileUrlResponse.Result;
PNStatus getFileUrlStatus = getFileUrlResponse.Status;
if (!getFileUrlStatus.Error && getFileUrlResult != null)
{
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(getFileUrlResult));
}
else
{
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(getFileUrlStatus));
}
Response
{
"Url":"http://ps.pndsn.com/v1/files/demo/channels/my_channel/files/d9515cb7-48a7-41a4-9284-f4bf331bc770/cat_picture.jpg?pnsdk=NET461CSharp4.9.0.0×tamp=1595771548&uuid=pn-9ce9e988-8e04-40bf-90c4-ebe170478f7d"
}
Returns
The GetFileUrl()
operation returns a PNResult<PNFileUrlResult>
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Result | PNFileUrlResult | Returns a PNFileUrlResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNFileUrlResult
contains the following properties:
Property Name | Type | Description |
---|---|---|
Url | string | URL which can be used to download remote file with specified name and identifier. |
Download file
Download file from specified Channel
.
Method(s)
pubnub.DownloadFile()
.Channel(string)
.FileId(string)
.FileName(string)
.CipherKey(string)
Parameter | Type | Required | Description |
---|---|---|---|
Channel | string | Yes | Name of channel within which file with name has been uploaded. |
FileId | string | Yes | Unique file identifier which has been assigned during file upload. |
FileName | string | Yes | Name under which uploaded file is stored for channel . |
CipherKey | string | Optional | Key which should be used to decrypt downloaded data. If CipherKey is not provided, SDK uses CipherKey of PNConfiguration . |
Basic Usage
PNResult<PNDownloadFileResult> fileDownloadResponse = await pubnub.DownloadFile()
.Channel("my_channel")
.FileId("d9515cb7-48a7-41a4-9284-f4bf331bc770")
.FileName("cat_picture.jpg")
.CipherKey("my_cipher_key")
.ExecuteAsync();
PNDownloadFileResult fileDownloadResult = fileDownloadResponse.Result;
PNStatus fileDownloadStatus = fileDownloadResponse.Status;
if (!fileDownloadStatus.Error && fileDownloadResult != null)
{
fileDownloadResult.SaveFileToLocal(downloadUrlFileName); //saves to bin folder if no path is provided
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(fileDownloadResult.FileName));
}
else
{
show all 17 linesResponse
{
//Call fileDownloadResult.SaveFileToLocal(<destination path>) to save file.
"FileBytes":"/9j/4AAQSkZJRgABAQEAkACQAAD/4RCERXhpZgAATU0AKgAAAAgABAE7AAIAAAAGAAAISodpAAQAAAABAAAIUJydAAEAAAA...<truncated due to lengthy data",
"FileName":"cat_picture.jpg"
}
Returns
The DownloadFile()
operation returns a PNResult<PNDownloadFileResult>
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Result | PNDownloadFileResult | Returns a PNDownloadFileResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNDownloadFileResult
contains the following properties:
Property Name | Type | Description |
---|---|---|
FileBytes | byte[] | byte array of the downloaded file. Use SaveFileToLocal method to copy file to local. |
FileName | string | Name of the downloaded file from channel. |
SaveFileToLocal(string) | Provide full destination path to SaveFileToLocal for saving the downloaded file locally. |
Delete file
Delete file from specified Channel
.
Method(s)
pubnub.DeleteFile()
.Channel(string)
.FileId(string)
.FileName(string)
Parameter | Type | Required | Description |
---|---|---|---|
Channel | string | Yes | Name of channel within which file with name needs to be deleted. |
FileId | string | Yes | Unique file identifier of the file to be deleted. |
FileName | string | Yes | Name of the file to be deleted from the channel . |
Basic Usage
PNResult<PNDeleteFileResult> deleteFileResponse = await pubnub.DeleteFile()
.Channel("my_channel")
.FileId("d9515cb7-48a7-41a4-9284-f4bf331bc770")
.FileName("cat_picture.jpg")
.ExecuteAsync();
PNDeleteFileResult deleteFileResult = deleteFileResponse.Result;
PNStatus deleteFileStatus = deleteFileResponse.Status;
if (!deleteFileStatus.Error && deleteFileResult != null)
{
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(deleteFileResult));
}
else
{
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(deleteFileStatus));
}
Response
{}
Returns
The DeleteFile()
operation returns a PNResult<PNDeleteFileResult>
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Result | PNDeleteFileResult | Returns a PNDeleteFileResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNDeleteFileResult
returns empty object
Publish file message
Publish file message from specified Channel
.
Method(s)
pubnub.PublishFileMessage()
.Channel(string)
.FileId(string)
.FileName(string)
.Message(object)
.Meta(Dictionary<string, object>)
.ShouldStore(bool)
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
Channel | string | Yes | Name of channel to publish file message | |
FileId | string | Yes | Unique file identifier of the file. | |
FileName | string | Yes | Name of the file. | |
Message | string | Optional | The payload. | |
Meta | string | Optional | Meta data object which can be used with the filtering ability. | |
ShouldStore | bool | Optional | true | Store in history . |
Basic Usage
PNResult<PNPublishFileMessageResult> publishFileMsgResponse = await pubnub.PublishFileMessage()
.Channel("my_channel")
.FileId("d9515cb7-48a7-41a4-9284-f4bf331bc770")
.FileName("cat_picture.jpg") //checks the bin folder if no path is provided
.Message("This is a sample message")
.ExecuteAsync();
PNPublishFileMessageResult publishFileMsgResult = publishFileMsgResponse.Result;
PNStatus publishFileMsgStatus = publishFileMsgResponse.Status;
if (!publishFileMsgStatus.Error && publishFileMsgResult != null)
{
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(publishFileMsgResult));
}
else
{
Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(publishFileMsgStatus));
show all 16 linesResponse
{
"Timetoken":15957738720237858
}
Returns
The PublishFileMessage()
operation returns a PNResult<PNPublishFileMessageResult>
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Result | PNPublishFileMessageResult | Returns a PNPublishFileMessageResult object. |
Status | PNStatus | Returns a PNStatus object. |
PNPublishFileMessageResult
contains the following properties:
Property Name | Type | Description |
---|---|---|
Timetoken | long | Returns a long representation of the timetoken when the message was published. |