File Upload
Table of contents
- Overview
- Enable file transfer in the admin console.
- Using Default File Upload Button
- Using Custom File Upload Button
Overview
This article will help you to support file upload.
You can checkout an implementation example here.
Enable file transfer in the admin console.
- Open https://admin.bold360.com/ and log in.
- Go to CHANNELS -> Chat -> Chat Windows -> Choose relevant window -> File Transfer section.
- Check
Enable
and choose:Agent to customer
,Customer to agent
.
Using Default File Upload Button
Register To Delegate
override func viewDidLoad() {
chatController.delegate = self
}
Implement Delegate Upload Trigger Event
In order to be notified when the user triggered the upload, you need to implement didClickUploadFile
in ChatControllerDelegate
.
extension FileUploadDemoViewController: ChatControllerDelegate {
func didClickUploadFile() {
// present file picker
}
}
Creating Upload Request
Once the file was picked create an upload request.
let request = UploadRequest()
request.fileName = (resources.first!).originalFilename
request.fileType = .picture
request.fileData = data
Uploading Files
/// Once upload request created start upload file process.
self.chatController.uploadFile(request, progress: { (progress) in
/// You can get progress values, to show upload progress bar.
print("application file upload progress -> %.5f", progress)
}) { (info: FileUploadInfo!) in
/// Call handle function on ChatController with upload file information.
// self.uploadBtn.removeFromSuperview() uncomment for custom file upload
self.chatController.handle(BoldEvent.fileUploaded(info))
}
Using Custom File Upload Button
To implement your own File Upload button: Listen to the relevant chat state (.pending) by implementing didUpdateState
in ChatControllerDelegate
.
extension FileUploadDemoViewController: ChatControllerDelegate {
func didUpdateState(_ event: ChatStateEvent!) {
switch event.state {
///a. Listen to relevant chat state
case .pending:
/// b. Validate file transfer enabled on bold admin console.
if(self.chatController.isFileTransferEnabled) {
/// c. Add custom button.
DispatchQueue.main.async {
self.uploadBtn.backgroundColor = .blue
self.uploadBtn.setTitle("Upload File", for: .normal)
self.uploadBtn.frame.size = CGSize(width: 150, height: 70)
self.uploadBtn.center = (self.navigationController?.visibleViewController?.view.center)!
self.uploadBtn.addTarget(self, action: #selector(self.uploadFile), for: .touchUpInside)
self.navigationController?.visibleViewController?.view.addSubview(self.uploadBtn)
}
}
break
default:
break
}
}
}
Note: File upload must be enabled in the admin console.