Unity V4 Files API Reference for Realtime Apps
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
Description
Upload file / data to specified channel
.
Method(s)
pubnub.SendFile().Channel(string).Message(string).CipherKey(string).Name(string).FilePath(string).TTL(int).ShouldStore(bool).Meta(Dictionary<string, string>).Async()
Parameter Type Required Description Channel
string Yes Channel
to upload the file.Message
string Optional Message which should be sent along with file to specified channel
FilePath
string Yes Full path of the file with file name. CipherKey
string Optional Key to be used to encrypt uploaded data. If not provided, PNConfiguration
CipherKey is used, if it exists.TTL
int Optional How long message should be stored in channel's storage. ShouldStore
bool Optional Whether PubNub published file message
should be stored inchannel
history. Defauts totrue
Meta
Dictionary<string, object> Optional Dictionary<string, object> with values which should be used by PubNub service to filter file messages.
Basic Usage
pubnub.SendFile().
Channel("my_channel").
Message("Look at this photo!").
Name("cat_picture.jpg").
FilePath("cat_picture.jpg").
Async((result, status) => {
Debug.Log(result);
if(!status.Error){
Debug.Log("result.Data.ID:" + result.Data.ID);
Debug.Log("result.Timetoken:" + result.Timetoken);
}
});
Returns
The SendFile()
operation returns a PNSendFileResult
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Data | PNFileData | See details below. |
Timetoken | long | Returns a long representation of the timetoken when the message was published. |
PNFileData
contains the following properties:
Property Name | Type | Description |
---|---|---|
ID | string | Returns the ID of the file. |
List channel files
Description
Retrieve list of files uploaded to Channel
.
Method(s)
pubnub.ListFiles().Channel(string).Limit(int).Next(string).Async()
Parameter Type Required Defaults 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
pubnub.ListFiles().Channel("my_channel").Async((result, status) => {
foreach(PNFileInfo pnFileInfo in result.Data){
Debug.Log(pnFileInfo.Name);
Debug.Log(pnFileInfo.ID);
Debug.Log(pnFileInfo.Size);
Debug.Log(pnFileInfo.Created);
}
});
Returns
The ListFiles()
operation returns a PNResult
Property Name | Type | Description |
---|---|---|
Data | PNFileInfo | See details below. |
Count | int | Number of files returned. |
Next | string | Cursor bookmark for fetching the next page. |
PNFileInfo
contains the following properties:
Property Name | Type | Description |
---|---|---|
Name | string | Name of the file. |
Id | string | If of the file. |
Size | int | Size of the file. |
Created | string | Date the file was created. |
Get File Url
Description
Generate URL which can be used to download file from target Channel
.
Method(s)
pubnub.GetFileURL().Channel(string).ID(string).Name(string).Async()
Parameter Type Required Description Channel
string Yes Name of channel
within whichfile
withname
has been uploaded.ID
string Yes Unique file identifier
which has been assigned during file upload.Name
string Yes Name under which uploaded file
is stored forchannel
.
Basic Usage
pubnub.GetFileURL().
Channel("my_channel").
ID("fileID").
Name("cat_picture.jpg").
Async((result, status) => {
Debug.Log(status.Error);
Debug.Log(result.URL);
Debug.Log(resultTimeToken);
});
Returns
The GetFileUrl()
operation returns a PNFileUrlResult
which 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
Description
Download file from specified Channel
.
Method(s)
pubnub.DownloadFile().Channel(string).CipherKey(string).ID(string).Name(string).SavePath(string).Async()
Parameter Type Required Description Channel
string Yes Name of channel
within which file withname
has been uploaded.ID
string Yes Unique file identifier which has been assigned during file upload. Name
string Yes Name under which uploaded file
is stored forchannel
.SavePath
string Optional Path to save the file at. CipherKey
string Optional Key which should be used to decrypt downloaded data. If CipherKey is not provided, SDK uses CipherKey of PNConfiguration
.
Basic Usage
pubnub.DownloadFile().
Channel("my_channel").
ID("fileID").
Name("cat_picture.jpg").
SavePath("cat_picture.jpg").
Async((result, status) => {
Debug.Log(status.Error);
});
The DownloadFile()
operation returns a type PNDownloadFileResult
which is empty.
Delete file
Description
Delete file from specified Channel
.
Method(s)
pubnub.DeleteFile().Channel(string).ID(string).Name(string).Async()
Parameter Type Required Description Channel
string Yes Name of channel
within which file with name needs to be deleted.ID
string Yes Unique file identifier of the file to be deleted. Name
string Yes Name of the file to be deleted from the channel
.
Basic Usage
pubnub.DeleteFile().
Channel("my_channel").
ID("fileID").
Name("cat_picture.jpg").
Async((result, status) => {
Debug.Log(status.Error);
});
The DeleteFile()
operation returns a type PNDeleteFileResult
which is empty.
Publish file message
Description
Publish file message from specified Channel
.
Method(s)
pubnub.PublishFileMessage().Channel(string).FileId(string).FileName(string).MessageText(string)
Parameter Type Required Defaults Description Channel
string Yes Name of channel
to publish file messageFileId
string Yes Unique file identifier of the file. FileName
string Yes Name of the file. MessageText
string Optional The payload of type PnPublishMessage
.
Basic Usage
pubnub.PublishFileMessage().
Channel("my_channel").
FileID("fileID").
FileName("cat_picture.jpg").
MessageText("Look at this photo!").
Async((presult, pstatus) => {
Debug.Log(presult.Timetoken);
});
Returns
The PublishFileMessage()
operation returns a PNPublishFileMessageResult
which contains the following properties:
Property Name | Type | Description |
---|---|---|
Timetoken | long | Returns a long representation of the timetoken when the message was published. |