[英]Swift 3 Alamofire multipart upload
Thanks to migration to Swift 3, I find it difficult to compile my project that uses Alamofire. 由于迁移到Swift 3,我发现很难编译使用Alamofire的项目。
The problem occurs when uploading multipartFormData: 上传multipartFormData时出现问题:
Alamofire.upload(.POST, URL, headers: headers, multipartFormData: {
multipartFormData in
.
.
.
})
Ambiguous reference to member 'upload(_:to:method:headers:)'
成员'上传的模糊参考(_: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 这就是在Swift 3中实现上传方法的方法
For example, using Alamofire 4.0.0 in Swift 3 : 例如,在Swift 3中使用Alamofire 4.0.0 :
(make sure you are 4.0.0 ready as it looks like you haven't updated your Alamofire yet) (确保你已准备好4.0.0,因为你好像尚未更新你的Alamofire)
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: 所以
headers
需要通过URL请求传递:
let URL = try! URLRequest(url: "http://example.com", method: .get, headers: headers)
In swift 3, trying to set multipartFormData as @DCDC pointed out in his solution. 在swift 3中,尝试将multipartFormData设置为@DCDC在他的解决方案中指出。 XCode try to cast to AnyObject before .data(), so instead of
XCode尝试在.data()之前强制转换为AnyObject,而不是
value.data(using: String.Encoding.utf8)!, withName: key
I did 我做到了
[replace_your_var_name_here].data(using: String.Encoding.utf8)!, withName: key
In my case my var list was not big so hardcoding was an option. 在我的情况下,我的var列表并不大,所以硬编码是一个选项。
For Swift 3 and Alamofire ~4.3.0 对于Swift 3和Alamofire~4.3.0
If someone like me tried to get request object synchronously (without using locks or dispatch_groups) you can use this approach: 如果像我这样的人试图同步获取请求对象(不使用lock或dispatch_groups),您可以使用以下方法:
// outer function
...
let string = "string to send"
let multipartFormData = MultipartFormData()
multipartFormData.append(string.data(using: .utf8)!, withName: "str")
guard let data = try? multipartFormData.encode() else {
// fail appropriately
}
let request = sessionManager.upload(data,
to: url,
method: .post,
/* this is VERY IMPORTANT LINE */ headers: ["Content-Type" : multipartFormData.contentType])
request.validate()
// do whatever you need with request
Please note that you need to set Content-Type
header from you multipartFormData
as it contains boundaries. 请注意,您需要从
multipartFormData
设置Content-Type
标头,因为它包含边界。
If you don't need to have your request object synchronously the other answer with 如果您不需要同步请求对象请使用其他答案
Alamofire.upload(multipartFormData: { (multipartFormData) in
is working as expected. 正在按预期工作。 In case of successful encoding of data it will return you request object in callback closure.
在成功编码数据的情况下,它将在回调闭包中返回请求对象。
IMPORTANT NOTE: if you use the method I have described, it will block your thread (in most cases you probably are in Main thread) to copy and encode your data. 重要说明:如果您使用我所描述的方法,它将阻止您的线程(在大多数情况下,您可能在主线程中)复制和编码您的数据。 So don't use it for large files or whatever.
所以不要将它用于大文件或其他任何东西。 It is async in Alamofire on purpose.
它故意在Alamofire中异步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.