作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Alamofire 从 TableView UI 将音频文件上传到服务器。
我的应用程序崩溃了,我想我在 multipartformdata 行中错误地连接了我的 URL,请帮助我如何正确设置。
@IBAction func Upload(_ sender: Any) {
let voiceData = try? Data(contentsOf: getDirectory())
let params : [String:String] = []
Alamofire.upload(
multipartFormData: { (multipartForm) in
multipartForm.append(voiceData!, withName: "voice", fileName: "file", mimeType: "audio/m4a")
for (key, value) in params {
multipartForm.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
},
usingThreshold : SessionManager.multipartFormDataEncodingMemoryThreshold,
to : "http://18.136.194.124/voupload.php",
method: .post){ (result) in
switch result {
'''''''
}
func getDirectory() -> URL
{
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentDirectory = paths[0]
return documentDirectory
}
@IBOutlet weak var mtable: UITableView!
@IBOutlet weak var Butlabel: UIButton!
var recordingSession:AVAudioSession!
var audiorecorder:AVAudioRecorder!
var audioPlayer:AVAudioPlayer!
var numberOfrecords = 0
@IBAction func RecordButton(_ sender: Any) {
if audiorecorder == nil
{
numberOfrecords += 1
let filename = getDirectory().appendingPathComponent("\(numberOfrecords).m4a")
let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 1200, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue]
// VOXIT STARTS
do
{
audiorecorder = try AVAudioRecorder(url: filename, settings: settings)
audiorecorder.delegate = self
audiorecorder.record()
Butlabel.setTitle("Stop Recording", for: .normal)
}
catch{
displayAlert(title: "Ups!", message: "Recording Failed")
}
}
else
{
// STOP rec
}
}
如果有人可以解释一下 MultiDataForm 代码。感谢任何帮助。
最佳答案
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(data, // the audio as Data
withName: "Filedata",
fileName: "file.m4a", // name of the file
mimeType: "audio/mpeg")
}, to: uploadUrl, encodingCompletion: { (result) in
switch result {
case .failure(let error):
completion(.failure(Error you want to throw))
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
if let error = response.error {
completion(.failure(Error you want to throw))
} else {
completion(.success(()))
}
}
}
})
关于ios - 使用 Alamofire 上传音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57248190/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!