Download a File
You can download a file from an existing folder in the File Store by calling the download() method. The operation will return a temporary URL where the file is stored.
You can download a file in one of the two ways shown below.
The <FILE_INSTANCE> used in both the code sections is an instance created for the specific file, as shown in the sample code snippet. This will refer to the file from the specific folder that must be downloaded.
Through Delegate
The file is downloaded through a delegate in this method, as shown in the code syntax below:
copy<FILE_INSTANCE>.download( fileDownloadDelegate : ZCatalystFileDownloadDelegate )
Parameters:
- FileDownloadDelegate: An instance of the type ZCatalystFileDownloadDelegate.
A sample code snippet is shown below:
copyfunc progress(fileRefId: String, session: URLSession, downloadTask: URLSessionDownloadTask, progressPercentage: Double, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) { print("The percentage of file download is \(progressPercentage)") } func didFinish( fileRefId : String, fileResult : ( Data, URL ) ) { print("The file with reference id " + fileRefId + " is successfully downloaded.") } func didFail( fileRefId : String, with error : ZCatalystError? ) { print("The file with reference id " + fileRefId + " download has been failed due to \(ZCatalystError.self)") } ZCatalystApp.shared.getFileStoreInstance().getFolderInstance(id : 105000000121098) .getFile(id : 332000000044009){(result) in //replace your folder id and file id here switch result{ case .success ( let file) : file.download( fileDownloadDelegate : self ) case .error( let error ) : print( "Error occurred >>> \( error )" ) } }
Through a Completion Handler
The file is downloaded through a completion handler in this method, as shown in the code syntax below:
copy<FILE_INSTANCE>.download( completion : @escaping ( Result< ( Data, URL ), ZCatalystError > ) -> Void )
Parameters:
- completion: If the operation is successful, the completion block will return the file data and a temporary URL where the file data is stored. Else, it will return an error.
A sample code snippet is shown below:
copyZCatalystApp.shared.getFileStoreInstance().getFolderInstance(id: 105000000121098).getFile(fileId: 332000000044009) {(result) in //Replace this with your Folder ID and File ID switch result{ case .success(let file) : file.download(){(result) in switch result{ case .success(let downloaded_file) : print("The file is successfully downloaded.") case .error(let error) : print( "Error occurred >>> \( error )" ) } } case .error(let error) : print( "Error occurred >>> \( error )" ) } }
Last Updated 2023-09-03 01:06:41 +0530 +0530
Yes
No
Send your feedback to us