How to check if multiple upload was successful with alamofire image

I am making a network call to upload an image to the server. I am currently using the following for code. This code works great when 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 move it offline. It will still fulfill this function and still fulfill the promise. Even the network is down. I think this is because it checks if the encodingResult is successful or not. NOt for the network call itself.

How can I check if the network call was successful or not? He returns the Void.

Best, Kevin.

Importing notes:

  • The server returns nill if we get 200 code, which means you have uploaded an image.
+3


source to share


1 answer


If you are going to use status codes to determine success or failure, you must add 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 after a successful response, you will also receive a JSON response. You may need to perform additional verification value

.

If you don't return JSON after success, just use response

instead 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)
            }
        }
    }
}

      

+2


source







All Articles