Upload a File

You can upload a file to an existing folder in the File Store. After the file is uploaded in the folder, a unique File ID will be generated for it. You can upload an image, text document, CSV, or any type of file you need upto 100MB of file size.

You can upload the file in one of the following ways, as discussed below. The <FOLDER_INSTANCE> used in the code sections of all the methods below is the instance defined in the Folder Instance page. This will refer to the folder that the file must be uploaded in.

Through Delegate

You can upload the file to the folder of the given folder instance through a delegate. This can be done in two ways:

i. By passing the file path

You can pass the File Reference ID and file path along with the delegate method object as the arguments to the upload() method:

    
copy
<FOLDER_INSTANCE>.upload( fileRefId : String, filePath : URL, fileUploadDelegate : ZCatalystFileUploadDelegate )

Parameters:

  • fileRefId: A unique reference ID to identify a specific upload task. This is returned in the Delegate method after the task is successfully completed.

  • filePath: The file path of the file to be uploaded in the local system.

  • fileUploadDelegate: An instance of the type ZCatalystFileUploadDelegate

A sample code snippet is shown below:

    
copy
func progress(fileRefId: String, session: URLSession, sessionTask: URLSessionTask, progressPercentage: Double, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) { print("The percentage of file upload is \(progressPercentage)") } func didFinish(fileRefId: String, fileDetails: ZCatalystFile) { print("The file with reference id " + fileRefId + " is successfully uploaded.") } func didFail(fileRefId: String, with error: ZCatalystError?) { print("The file with reference id " + fileRefId + " upload has been failed due to \(ZCatalystError.self)") } } let filePath = self.testBundle?.path( forResource : "companylogo", ofType : "jpeg" ) //Replace this with your file name let url = URL( fileURLWithPath : filePath! ) ZCatalystApp.shared.getFileStoreInstance().getFolderInstance( id : 2823000000006544 ).upload( fileRefId : fileRefId, filePath : url, fileUploadDelegate : self ) //Replace this with your Folder ID

ii. By passing the file data

You can pass the file data along with the delegate method object as the arguments to the upload() method:

    
copy
<FOLDER_INSTANCE>.upload( fileRefId : String, fileName : String, fileData : Data, fileUploadDelegate : ZCatalystFileUploadDelegate )

Parameters:

  • fileRefId: A unique reference ID to identify a specific upload task. This is returned in the Delegate method after the task is successfully completed.

  • fileName: The name of the file to be uploaded.

  • fileData: The contents of the file of the type Data

  • fileUploadDelegate: An instance of the type ZCatalystFileUploadDelegate

A sample code snippet is shown below:

    
copy
func progress(fileRefId: String, session: URLSession, sessionTask: URLSessionTask, progressPercentage: Double, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) { print("The percentage of file upload is \(progressPercentage)") } func didFinish(fileRefId: String, fileDetails: ZCatalystFile) { print("The file with reference id " + fileRefId + " is successfully uploaded.") } func didFail(fileRefId: String, with error: ZCatalystError?) { print("The file with reference id " + fileRefId + " upload has been failed due to \(ZCatalystError.self)") } } if let data = image?.pngData() ZCatalystApp.shared.getFileStoreInstance().getFolderInstance( id : 105000000121098 ).upload( fileRefId : "1234567", fileName : url.lastPathComponent, fileData: data, fileUploadDelegate : self ) //Replace this with your File Reference ID

Through Completion Handler

You can upload the file to the folder of the given folder instance through a completion handler. This can be done in two ways:

i. By passing the file path

You can pass the file path along with the completion handler as the arguments to the upload() method:

    
copy
<FOLDER_INSTANCE>.upload( filePath : URL, completion : @escaping ( Result< ZCatalystFile, ZCatalystError > ) -> Void )

Parameters:

  • filePath: The file path of the file to be uploaded in the local system.

  • completion: If the operation is successful, the completion block will return the details of the uploaded file. Else, it will return an error.

A sample code snippet is shown below:

    
copy
let filePath = self.testBundle?.path( forResource : "document", ofType : "jpeg" ) //Replace this with your file name let url = URL( fileURLWithPath : filePath! ) ZCatalystApp.shared.getFileStoreInstance().getFolderInstance( id : 105000000121098 ).upload( filePath : url) { ( fileResult ) in //Replace this with your Folder ID switch fileResult{ case .success (let file) : print ("Image has been successfully uploaded and it's id is " + file.id) case .error(let error) : print( "Error occurred >>> \( error )" ) } }

ii. By passing the file data

You can pass the file data along with the completion handler as the arguments to the upload() method:

    
copy
<FOLDER_INSTANCE>.upload( fileName : String, fileData : Data, completion: @escaping (Result<ZCatalystFile, ZCatalystError>) -> Void )

Parameters:

  • fileName: The name of the file to be uploaded.

  • fileData: The contents of the file of the type Data.

  • completion: If the operation is successful, the completion block will return the details of the uploaded file. Else, it will return an error.

A sample code snippet is shown below:

    
copy
let image = UIImage ( named : "document" ) //replace your file name here if let data = image?.pngData(){ ZCatalystApp.shared.getFileStoreInstance().getFolderInstance(id: 105000000121098).upload(fileName: "document", fileData: data, fileRefId : "123456789983") { (result) in //replace your file name and fire reference id here switch result{ case .success (let file) : print ("Image has been successfully uploaded and it's id is \(file.id)") case .error(let error) : print( "Error occurred >>> \( error )" ) } } }

Last Updated 2023-09-03 01:06:41 +0530 +0530