[英]How to check if the multipart upload was successful with alamofire image
I am making a network call to upload an image to a backend server. 我正在进行网络调用以将图像上传到后端服务器。 Right now I am using the following to code.
现在我正在使用以下代码。 This code works perfectly in conditions where the internet is online.
此代码适用于互联网在线的情况。
// MARK: - PUT
static func PUT(URL: String,
data: Data,
mimeType: String,
headers: [String: String]?) -> Promise<Void>
{
return Promise { fulfill, reject in
let URL = try! URLRequest(url: URL, method: .put, headers: headers)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(InputStream.init(data: data), withLength: UInt64(data.count), name: "image", fileName: "file.png", mimeType: mimeType)
},
with: URL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
if response.result.value == nil {
fulfill()
}else {
reject(response.result.error!)
}
}
case .failure( _):
break
}
})
}
}
}
In case I put it on offline mode. 如果我把它放在离线模式。 It'll still execute the function and still fullfills() the promise.
它仍将执行该函数并仍然满足()承诺。 Even tho the network is offline.
即使网络处于脱机状态。 I think this is because it's checking if the encodingResult is succesful or not.
我认为这是因为它正在检查encodingResult是否成功。 NOt for the network call itself.
NOt用于网络呼叫本身。
How am I able to check if the network call was successful or not? 我如何检查网络呼叫是否成功? It's returning Void.
它正在回归虚空。
Import notes: 导入说明:
If you're going to use status codes to determine success or failure, you should add validate
: 如果您要使用状态代码来确定成功或失败,则应添加
validate
:
For example: 例如:
static func PUT(URL: String,
data: Data,
mimeType: String,
headers: [String: String]?) -> Promise<Void> {
return Promise { fulfill, reject in
let URL = try! URLRequest(url: URL, method: .put, headers: headers)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(data, withName: "image", fileName: "file.png", mimeType: mimeType)
}, with: URL) { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload
.validate(statusCode: 200 ..< 300)
.responseJSON { response in
switch response.result {
case .success:
fulfill()
case .failure(let error):
reject(error)
}
}
case .failure(let error):
reject(error)
}
}
}
}
The above assumes that upon successful response, you'll also get JSON response. 以上假设成功响应后,您还将获得JSON响应。 You might want to do some additional checking of
value
. 您可能想要进行一些额外的
value
检查。
If you're not returning JSON upon success, then just use response
, not responseJSON
: 如果您在成功时没有返回JSON,那么只需使用
response
,而不是responseJSON
:
static func PUT(URL: String,
data: Data,
mimeType: String,
headers: [String: String]?) -> Promise<Void> {
return Promise { fulfill, reject in
let URL = try! URLRequest(url: URL, method: .put, headers: headers)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(data, withName: "image", fileName: "file.png", mimeType: mimeType)
}, with: URL) { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload
.validate(statusCode: 200 ..< 300)
.response { response in
if let error = response.error {
reject(error)
} else {
fulfill()
}
}
case .failure(let error):
reject(error)
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.