Links

File Handling

Files are an essential component of modern software applications. Amity provides a powerful file management system that enables you to easily handle different types of files, such as document files, videos, and audio files. In this section, we will introduce you to the concept of a file in Amity and provide an overview of file handling in Amity.

File Data

Property
Description
fileId
Root file key on cloud storage
fileUrl
HTTP link for file download
type
File type
createdAt
Date/time when a file is uploaded
updatedAt
Date/time when a file is updated
attributes
Information about the file

Upload Files

To upload a file to the system, you can use the Amity File Upload API provided by the SDK. The API allows you to upload a file to the Amity server by providing the file's data and the file metadata, such as the file name, file type, and file size. The SDK simplifies the process of uploading files by providing pre-built components that you can easily integrate into your application.
iOS
Android
JavaScript
TypeScript
Flutter
// Uploading File from URL
fileRepository.uploadFile(with: <URL>, fileName: "myfile.pdf") { progress in
// ...
} completion: { fileData, error in
// Handle completion here
}
// Uploading File Binary
let fileToUpload = AmityUploadableFile(...)
fileRepository.uploadFile(fileToUpload) { progress in
// ...
} completion: { fileData, error in
// Handle completion here
}
On Android, you can separately observe uploading states outside of the uploading method by using:
import { FileRepository, LoadingStatus } from '@amityco/js-sdk';
const liveObject = FileRepository.createFile({
file, // https://developer.mozilla.org/en-US/docs/Web/API/File
onProgress: ({ currentFile, currentPercent }) => {
},
});
liveObject.on('loadingStatusChanged', ({ newValue }) => {
if (newValue === LoadingStatus.Loaded) {
console.log('The file is uploaded', liveObject.model);
}
});
liveObject.on('dataError', (error) => {
console.error('can not upload the file', error);
});
// upload video
const liveObject = FileRepository.createVideo({
file,
onProgress: ({ currentFile, currentPercent }) => {
},
});
Refer to the
file model
for the description of the response after a successful file creation. If an error is encountered while creating the file, it will return the following errors:
//Attached file payload is too large
{
"status": "error",
"code": 500000,
"message": "Payload too large."
}
// Unexpected error
{
"status": "error",
"code": 500000,
"message": "Unexpected error"
}

Retrieve Files

You can retrieve a file from Amity using the Amity File Retrieval API provided by the SDK. The API enables you to retrieve a file from the Amity server by supplying the file ID. The SDK streamlines the process of retrieving files by offering pre-made components that can be smoothly integrated into your app.
iOS
Android
JavaScript
TypeScript
Flutter
There are two ways to download files on iOS.
  1. 1.
    Using AmityFileRepository
    FileRepository class provides dedicated downloadFile(..) method to download files uploaded using same class.
    Note: This class does not perform any kind of caching for downloaded files.
  2. 2.
    Using any third party library or your own implementation
    You can download the file directly from provided fileUrl using your own implementation or any custom third party libraries of your choice.
// Download file directly into filesystem
fileRepository.downloadFile(fromURL: <url>) { downloadLocationUrl, error> in
// Handle completion
}
// Download file & provide it as Data
fileRepository.downloadFileAsData(fromURL: <url>) { data, error> in
// Handle Completion
}
Supported ✅ (please wait while we prepare a real example!)
import { FileRepository } from '@amityco/js-sdk';
let file;
const liveObject = FileRepository.getFile(fileId);
liveObject.on('dataUpdated', (updatedModel) => {
file = updatedModel;
});
liveObject.on('dataError', (error) => {
console.error('Can not get the file', error);
});
file = liveObject.model;
Supported ✅ (please wait while we prepare a real example!)
Supported ✅ (please wait while we prepare a real example!)

Delete Files

In addition to uploading and retrieving files, Amity provides a deleting function to delete a file that is no longer needed.
iOS
Android
JavaScript
TypeScript
Flutter
Supported ✅ (please wait while we prepare a real example!)
Supported ✅ (please wait while we prepare a real example!)
import { FileRepository } from '@amityco/js-sdk';
const success = await FileRepository.deleteFile(fileId);
The response will return true if the file deletion is successful.
The response will return true if the file deletion is successful.
"data": {
"success": true
}
Otherwise, if an error is encountered during the deletion, it will return the following errors:
//Permission denied error
{
"status": "error",
"code": 400301,
"message": "Permission denied"
}
Resource Not Found error
{
"status": "error",
"code": 400400,
"message": "Resource Not Found."
}
//Passed the wrong parameters error
{
"status": "error",
"code": 500000,
"message": "Parameters error."
}
Supported ✅ (please wait while we prepare a real example!)