C# C# V4 Files API Reference for Realtime Apps

 

These docs are for PubNub 4.0 for C# which is our latest and greatest! For the docs of the 3.x versions of the SDK, please check the links: C#, Windows 8, Windows 8.1, ASP.Net, Windows Phone 8, Windows Phone 8.1, Xamarin.iOS, Xamarin.Android, Xamarin.Mac and C# PCL.

If you have questions about the PubNub for C# SDK, please contact us at support@pubnub.com.

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.

Upload file / data to specified channel.
  1. ParameterTypeRequiredDescription
    ChannelstringYesChannel for the file.
    FilestringYesFull path of the file with file name.
    CipherKeystringOptionalKey to be used to encrypt uploaded data. If not provided, PNConfiguration CipherKey will be used, if provided.
    TtLintOptionalHow long message should be stored in channel's storage.
    ShouldStoreboolOptionalWhether PubNub published file message should be stored in channel history.
    MessagestringOptionalMessage which should be sent along with file to specified channel.
    MetaDictionary<string, object>OptionalDictionary<string, object> with values which should be used by PubNub service to filter file messages.
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));
}
{
    "Timetoken":15957709330808500,
    "FileId":"d9515cb7-48a7-41a4-9284-f4bf331bc770",
    "FileName":"cat_picture.jpg"
}

The SendFile() operation returns a PNResult<PNFileUploadResult> which contains the following properties:

Property NameTypeDescription
ResultPNFileUploadResultReturns a PNFileUploadResult object.
StatusPNStatusReturns a PNStatus object.

PNFileUploadResult contains the following properties:

Property NameTypeDescription
TimetokenlongReturns a PNFileUploadResult object.
FileIdstringReturns the ID of the file.
FileNamestringReturns the name of the file.
Retrieve list of files uploaded to Channel.
  1. ParameterTypeRequiredDefaultsDescription
    ChannelstringYesChannel to get list of files.
    LimitintOptional100Number of files to return.
    NextstringOptionalPreviously-returned cursor bookmark for fetching the next page.
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));
}
{
    "FilesList":[
    {
        "Name":"cat_picture.jpg",
        "Id":"d9515cb7-48a7-41a4-9284-f4bf331bc770",
        "Size":25778,
        "Created":"2020-07-26T13:42:06Z"
    }],
    "Count":1,
    "Next":null
}

The ListFiles() operation returns a PNResult<PNListFilesResult> which contains the following properties:

Property NameTypeDescription
ResultPNListFilesResultReturns a PNListFilesResult object.
StatusPNStatusReturns a PNStatus object.

PNListFilesResult contains the following properties:

Property NameTypeDescription
FilesListlist<PNFileResult>List of channel files.
CountintNumber of files returned.
NextstringCursor bookmark for fetching the next page.

PNFileResult contains the following properties:

Property NameTypeDescription
NamestringName of the file.
IdstringID of the file.
SizeintSize of the file.
CreatedstringCreate date of the file.

Generate URL which can be used to download file from target Channel.

  1. ParameterTypeRequiredDescription
    ChannelstringYesName of channel within which file with name has been uploaded.
    FileIdstringYesUnique file identifier which has been assigned during file upload.
    FileNamestringYesName under which uploaded file is stored for channel.
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));
}
{
    "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&timestamp=1595771548&uuid=pn-9ce9e988-8e04-40bf-90c4-ebe170478f7d"
}

The GetFileUrl() operation returns a PNResult<PNFileUrlResult> which contains the following properties:

Property NameTypeDescription
ResultPNFileUrlResultReturns a PNFileUrlResult object.
StatusPNStatusReturns a PNStatus object.

PNFileUrlResult contains the following properties:

Property NameTypeDescription
UrlstringURL which can be used to download remote file with specified name and identifier.
Download file from specified Channel.
  1. ParameterTypeRequiredDescription
    ChannelstringYesName of channel within which file with name has been uploaded.
    FileIdstringYesUnique file identifier which has been assigned during file upload.
    FileNamestringYesName under which uploaded file is stored for channel.
    CipherKeystringOptionalKey which should be used to decrypt downloaded data. If CipherKey is not provided, SDK uses CipherKey of PNConfiguration.
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
{
    Console.WriteLine(pubnub.JsonPluggableLibrary.SerializeToJsonString(fileDownloadStatus));
}
{
    //Call fileDownloadResult.SaveFileToLocal(<destination path>) to save file.
    "FileBytes":"/9j/4AAQSkZJRgABAQEAkACQAAD/4RCERXhpZgAATU0AKgAAAAgABAE7AAIAAAAGAAAISodpAAQAAAABAAAIUJydAAEAAAA...<truncated due to lengthy data",
    "FileName":"cat_picture.jpg"
}

The DownloadFile() operation returns a PNResult<PNDownloadFileResult> which contains the following properties:

Property NameTypeDescription
ResultPNDownloadFileResultReturns a PNDownloadFileResult object.
StatusPNStatusReturns a PNStatus object.

PNDownloadFileResult contains the following properties:

Property NameTypeDescription
FileBytesbyte[]byte array of the downloaded file. Use SaveFileToLocal method to copy file to local.
FileNamestringName of the downloaded file from channel.
SaveFileToLocal(string)Provide full destination path to SaveFileToLocal for saving the downloaded file locally.
Delete file from specified Channel.
  1. ParameterTypeRequiredDescription
    ChannelstringYesName of channel within which file with name needs to be deleted.
    FileIdstringYesUnique file identifier of the file to be deleted.
    FileNamestringYesName of the file to be deleted from the channel.
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));
}
{}

The DeleteFile() operation returns a PNResult<PNDeleteFileResult> which contains the following properties:

Property NameTypeDescription
ResultPNDeleteFileResultReturns a PNDeleteFileResult object.
StatusPNStatusReturns a PNStatus object.

PNDeleteFileResult returns empty object

Publish file message from specified Channel.
  1. ParameterTypeRequiredDefaultsDescription
    ChannelstringYesName of channel to publish file message
    FileIdstringYesUnique file identifier of the file.
    FileNamestringYesName of the file.
    MessagestringOptionalThe payload.
    MetastringOptionalMeta data object which can be used with the filtering ability.
    ShouldStoreboolOptionaltrueStore in history.
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));
}
{
    "Timetoken":15957738720237858
}

The PublishFileMessage() operation returns a PNResult<PNPublishFileMessageResult> which contains the following properties:

Property NameTypeDescription
ResultPNPublishFileMessageResultReturns a PNPublishFileMessageResult object.
StatusPNStatusReturns a PNStatus object.

PNPublishFileMessageResult contains the following properties:

Property NameTypeDescription
TimetokenlongReturns a long representation of the time token when the message was published.