Swift 3 Alamofire 分段上传 [英] Swift 3 Alamofire multipart upload
问题描述
由于迁移到 Swift 3,我发现很难编译我使用 Alamofire 的项目.
上传multipartFormData时出现问题:
Alamofire.upload(.POST, URL, headers: headers, multipartFormData: {多部分表单数据...})
<块引用>
对成员'upload(_:to:method:headers:)'的不明确引用
非常感谢任何帮助,提前致谢!
已解决:
Alamofire.upload(multipartFormData: { (multipartFormData) inmultipartFormData.append(fileData, withName: "file_pack", fileName: "file_pack", mimeType: "text/plain")for (key, value) in self.parameters {multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)}}, with: URL2, encodingCompletion: { (result) in切换结果{案例.success(让上传,_,_):upload.responseJSON { 响应输入self.delegate?.showSuccessAlert()print(response.request)//原始 URL 请求打印(响应.响应)//URL响应打印(响应数据)//服务器数据print(response.result)//响应序列化的结果//self.showSuccesAlert()self.removeImage("frame", fileExtension: "txt")如果让 JSON = response.result.value {打印(JSON:(JSON)")}}案例.失败(让编码错误):self.delegate?.showFailAlert()打印(编码错误)}})
这是在 Swift 3 中应该如何实现上传方法
例如,在 Swift 3 中使用 Alamofire 4.0.0:
(确保您已准备好 4.0.0,因为您似乎还没有更新您的 Alamofire)
Alamofire.upload(multipartFormData: { (multipartFormData) in//代码}, to: URL, encodingCompletion: { (result) in//代码})
或
Alamofire.upload(multipartFormData: { (multipartFormData) in//代码}, with: URL, encodingCompletion: { (result) in//代码})
所以headers
需要通过URL请求传递:
让 URL = 尝试!URLRequest(url: "http://example.com", method: .get, headers: headers)
Thanks to migration to Swift 3, I find it difficult to compile my project that uses Alamofire.
The problem occurs when uploading multipartFormData:
Alamofire.upload(.POST, URL, headers: headers, multipartFormData: {
multipartFormData in
.
.
.
})
Ambiguous reference to member 'upload(_:to:method:headers:)'
Any help much appreciated, thanks in advance!
RESOLVED:
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(fileData, withName: "file_pack", fileName: "file_pack", mimeType: "text/plain")
for (key, value) in self.parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, with: URL2, encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
self.delegate?.showSuccessAlert()
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
self.removeImage("frame", fileExtension: "txt")
if let JSON = response.result.value {
print("JSON: (JSON)")
}
}
case .failure(let encodingError):
self.delegate?.showFailAlert()
print(encodingError)
}
})
This is how upload method should be implemented in Swift 3
For example, using Alamofire 4.0.0 in Swift 3:
(make sure you are 4.0.0 ready as it looks like you haven't updated your Alamofire yet)
Alamofire.upload(multipartFormData: { (multipartFormData) in
// code
}, to: URL, encodingCompletion: { (result) in
// code
})
or
Alamofire.upload(multipartFormData: { (multipartFormData) in
// code
}, with: URL, encodingCompletion: { (result) in
// code
})
So headers
need to be passed by URL request:
let URL = try! URLRequest(url: "http://example.com", method: .get, headers: headers)
这篇关于Swift 3 Alamofire 分段上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!