Reputation: 773
I want to upload recorded audio to the server and I was wondering how to convert to binary Data I know for images there is pngData
is there something smiler for Audio , I try the below code ,but I am not sure if the is the correct way
let fileData = try NSData(contentsOf: filePath!, options: NSData.ReadingOptions.mappedIfSafe)
let base64String = fileData.base64EncodedData(options: .lineLength76Characters)
Upvotes: 2
Views: 3028
Reputation: 100503
You need to convert it to data like this
guard let data = try? Data(contentsOf:fileUrl) else { return }
and upload as a multipart with Alamofire
Alamofire.upload(multipartFormData: { multipartFormData in
// use this
multipartFormData.append(data, withName: "audio", fileName: "audio.aac", mimeType: "audio/aac")
// or this
multipartFormData.append(fileUrl, withName: "audio", fileName: "audio.aac", mimeType: "audio/aac")
} ,to: url,method:.post,
headers:head,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (Progress) in
print("Upload Progress: \(Progress.fractionCompleted)")
})
upload.responseJSON { response in
}
break
case .failure(let encodingError):
break
}
})
Upvotes: 4