How to cancel Alamofire.upload - swift

I am uploading images on server via Alamofire.upload as multipart data. Unlike Alamofire.request it's not returning Request object, which I usually use to cancel requests.
But it's very reasonable to be able to cancel such a consuming requests like uploading. What are the options for this in Alamofire?

Using the Uploading MultiPartFormData example from the Alamofire README:
Alamofire.upload(
.POST,
"https://httpbin.org/post",
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn")
multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .Failure(let encodingError):
print(encodingError)
}
}
)
Here, upload.responseJSON returns a Request, which should allow you to assign it to something for cancellation later. For example:
let request = upload.responseJSON { ...
...
request.cancel()

I'm afraid you can't, according to the Alamofire source code the upload function returns an Request type in all of its overloads except in these that support MultipartFormData see the following code:
// MARK: MultipartFormData
/**
Creates an upload request using the shared manager instance for the specified method and URL string.
- parameter method: The HTTP method.
- parameter URLString: The URL string.
- parameter headers: The HTTP headers. `nil` by default.
- parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
- parameter encodingMemoryThreshold: The encoding memory threshold in bytes. `MultipartFormDataEncodingMemoryThreshold`
by default.
- parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
*/
public func upload(
method: Method,
URLString: URLStringConvertible,
headers: [String: String]? = nil,
multipartFormData: MultipartFormData -> Void,
encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
{
return Manager.sharedInstance.upload(
method,
URLString,
headers: headers,
multipartFormData: multipartFormData,
encodingMemoryThreshold: encodingMemoryThreshold,
encodingCompletion: encodingCompletion
)
}
/**
Creates an upload request using the shared manager instance for the specified method and URL string.
- parameter URLRequest: The URL request.
- parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
- parameter encodingMemoryThreshold: The encoding memory threshold in bytes. `MultipartFormDataEncodingMemoryThreshold`
by default.
- parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
*/
public func upload(
URLRequest: URLRequestConvertible,
multipartFormData: MultipartFormData -> Void,
encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
{
return Manager.sharedInstance.upload(
URLRequest,
multipartFormData: multipartFormData,
encodingMemoryThreshold: encodingMemoryThreshold,
encodingCompletion: encodingCompletion
)
}
It's recommended when you are writing client-side code, use multipart/form-data when your form includes any <input type="file"> elements.
So if you want to just upload an image to the server you can use the another upload function overloads that returns an Request object and you can cancel it like in this ways proposed in the Alamofire documentation:
let fileURL = NSBundle.mainBundle().URLForResource("Default", withExtension: "png")
ler request =Alamofire.upload(.POST, "https://httpbin.org/post", file: fileURL)
// request.cancel()
I hope this help you.

It's possible to prepare a closure, and transfer a request out of "encodingCompletion"
class NetworkManager {
private var closure: ((Request)->())?
func startUpload() {
Alamofire.upload(
.POST,
"https://httpbin.org/post",
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(fileURL: unicornImageURL, name: "unicorn")
multipartFormData.appendBodyPart(fileURL: rainbowImageURL, name: "rainbow")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
self.closure?(upload)
upload.responseJSON { response in
debugPrint(response)
uploadRequest = nil
}
case .Failure(let encodingError):
print(encodingError)
}
}
)
}
}

In my case I created "sessionManager" instance in my API class and assigned Alamofire's session manager with configuration to it.
var sessionManager: SessionManager!
// Setup request manager
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForResource = TimeInterval(15.0)
configuration.timeoutIntervalForRequest = TimeInterval(15.0)
sessionManager = Alamofire.SessionManager(configuration: configuration)
sessionManager.upload(multipartFormData: { multipartFormData in
// AppendMultipart parts
multipartFormData.append(metadataBodyPart, withName: PartName.Metadata, mimeType: MimeType.MultiPart)
multipartFormData.append(imageDataBodyPart, withName: PartName.Photo, mimeType: MimeType.ImageJPG)
} // ... rest of the code
Then I could create a method to cancel any current request type. Note that here "(_, uploadTasks, _)" you can also have "dataTasks" & "downloadTasks" which you can also cancel if you want "(dataTasks, uploadTasks, downloadTasks)"
func cancelUploadRequest() {
sessionManager.session.getTasksWithCompletionHandler { (_, uploadTasks, _) in
uploadTasks.forEach { $0.cancel() }
}
}
You can also have smth like this:
func cancel(request: CancelRequestType) {
sessionManager.session.getTasksWithCompletionHandler { (dataTasks, uploadTasks, downloadTasks) in
switch request {
case .DataTask:
dataTasks.forEach { $0.cancel() }
print("- - - Data task was canceled!")
case .UploadTask:
uploadTasks.forEach { $0.cancel() }
print("- - - Upload task was canceled!")
case .DownloadTask:
downloadTasks.forEach { $0.cancel() }
print("- - - Download task was canceled!")
case .ZeroTask:
print("- - - Zero tasks was found!")
}
}
}
Where "CancelRequestType" is enum. So you can call the method like API.cancel(request: .UploadTask)
enum CancelRequestType: String {
case DownloadTask = "DownloadTask"
case DataTask = "DataTask"
case UploadTask = "UploadTask"
case ZeroTask = "Zero"
}

Related

how to use form data in Alamofire

In the postman in the body section in the form-data part when I pass a mobile number as a key and mobile number as Int value I get a response. But when I did it in the code I will not get the response as expected.
my code is
func fetchRegisterData(){
let url = registerApi
var mobileNumber = mobilenumberTextfield.text
let parameters = ["mobile" : mobileNumber] as [String : Any]
AF.request(url,method: .post, parameters: parameters, encoding:JSONEncoding.default).responseJSON
{ response in switch response.result {
case .success(let JSON):
print("response is :\(response)")
case .failure(_):
print("fail")
}
}
}
If you're trying to pass parameters as multipart/form-data you have to use the upload method in Alamofire:
func fetchRegisterData() {
let parameters = ["mobile": mobilenumberTextfield.text!]
AF.upload(multipartFormData: { (multiFormData) in
for (key, value) in parameters {
multiFormData.append(Data(value.utf8), withName: key)
}
}, to: registerApi).responseJSON { response in
switch response.result {
case .success(let JSON):
print("response is :\(response)")
case .failure(_):
print("fail")
}
}
}
For normal Alamofire request -
While passing values as a parameter in Alamofire Request. You need to know about the data type of the request type values which is valid for API.
At API end, these values are getting parsed based on some data types. There must be some validations on the data type at the API side.
for mobileNumber, it can be either Int or String
1 - let parameters = ["mobile" : mobileNumber] as [String : Int]
2 - let parameters = ["mobile" : mobileNumber] as [String : String]
For multipart form data request use have to use something like below. However, if you are not uploading anything you should not use it. Ask API team to make changes in the API and normal parameters request
Alamofire.upload(multipartFormData: { (multipartFormData) in
//Try this
multipartFormData.append(mobileNumber, withName: "mobile")
//or this
multipartFormData.append("\(String(describing: mobileNumber))".data(using: .utf8)!, withName: "mobile")
}, usingThreshold: 10 * 1024 * 1024, to: apiUrl, method: .post, headers: [:], encodingCompletion: { (encodingResult) in
switch encodingResult {
case .success(let upload, _, _):
case .failure( _):
}
})

Multipart form data upload with Alamofire shows file missing in server

I'm trying to upload an image using Alamofire, the response shows success but the picture doesn't get uploaded. When I debugged with backend developer, it seemed the file attachment is missing in the request. However, the progress shows uploading details of the file. Can anyone help what's going wrong here.
class ImageUploadClient {
class func upload(image: UIImage, to request: URLRequest) {
let imgData = UIImageJPEGRepresentation(image, 0.5)!
let filename = "file.jpeg"
Alamofire.upload(multipartFormData: { (multiPartData) in
multiPartData.append(imgData, withName: filename, mimeType: "image/jpg")
}, usingThreshold: UInt64(1024),
with: request, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let request, let streamingFromDisk, let fileURL):
debugPrint(streamingFromDisk) // Shows true
debugPrint(fileURL) // Returns file url
debugPrint(request)
// upload progress closure
request.uploadProgress(closure: { (progress) in
print("upload progress: \(progress.fractionCompleted)")
// here you can send out to a delegate or via notifications the upload progress to interested parties
})
// response handler
request.validate()
.responseJSON(completionHandler: { (response) in
switch response.result {
case .success(let value):
debugPrint(value)
case .failure(let err):
debugPrint(err)
}
})
// encodingResult failure
case .failure(let error):
debugPrint(error)
}
})
}
}
try by adding file name for your image
like this
and withName key will contain Key name to your image on server
let profileKey = "profileImage"
multiPartData.append(imgData, withName: profileKey, fileName: filename, mimeType: "image/jpg")

Alamofire photo upload with key of the file in body

I am able to upload a photo via Postman but in my iOS application it fails, and it is so weird that I still get a .success from encodingCompletion.
Here is part of my code
UpdateUserInfo
//
// Update user info
//
func updateUserInfo(){
var imageData: Data!
var url = ""
if let userId = KeychainWrapper.standard.string(forKey: USER_ID_KEY){
url = URL_USER_UPLOAD_PIC + userId
}
if pickedImage != nil{
imageData = UIImagePNGRepresentation(pickedImage)
//imageData = UIImageJPEGRepresentation(pickedImage!, 1.0)
}
let token = KeychainWrapper.standard.string(forKey: USER_LOGIN_TOKEN_KEY)!
let headers: HTTPHeaders = [
"Authorization": "Bearer \(token)"
]
if imageData != nil{
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imageData!, withName: "fileset", fileName: "file.png", mimeType: "image/png")
}, to: url,
method: .post,
headers: headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
print("Donkey Success \(String(describing: upload.response?.statusCode))")
upload.responseString(completionHandler: { (response) in
debugPrint(response)
})
case .failure(let encodingError):
print(encodingError)
print("Donkey Fail")
}
})
}
}
and in my postman I have
Postman
My first question is why I am getting a .success if it fails to upload?
and my second question is, do I need to put the key "pic" (seen in Postman) somewhere in my request? if so where?
Thanks for any help in advance
In Post man Image Key is pic but in your code its "fileset"
change to
multipartFormData.append(imageData!, withName: "pic", fileName: "file.png", mimeType: "image/png")
And success is Result of encoding not uploading processs
EncodingResult is MultipartFormDataEncodingResult that Defines
whether the MultipartFormData encoding was successful and contains
result of the encoding as associated values. - Success: Represents a successful MultipartFormData encoding and contains the
new UploadRequest along with
streaming information. - Failure: Used to represent a failure in the MultipartFormData encoding and also
contains the encoding

Type of expression is ambiguous without more context in Alamofire.upload swift 3

Updated Alamofire 4.0.0 does not mention how to put Httpmethod & Httpheaders in upload with multipartFormData. That's why I google and found solution in that stackoverflow question. But the problem is I did same as that answer then got following error message and building is failed. Please help me how to solve it.
Type of expression is ambiguous without more context
Here is my coding:
let URL = try! URLRequest(url: Config.imageUploadURL, method: .post, headers: headers)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(self.imageData, withName: "image", fileName: "file.png", mimeType: "image/png")
},
to: URL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
if((response.result.value) != nil) {
} else {
}
}
case .failure( _):
}
}
)
Alamofire.upload(multipartFormData:to:encodingCompletion:) takes a URLConvertible for the to: argument. Instead, you should use Alamofire.upload(multipartFormData:with:encodingCompletion:) which takes a URLRequestConvertible for its with: argument.
I think your argument name of URL that is the same as the type URL() helps in creating strange compiler errors.
The following compiles for me:
let url = try! URLRequest(url: URL(string:"www.google.com")!, method: .post, headers: nil)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(Data(), withName: "image", fileName: "file.png", mimeType: "image/png")
},
with: url,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
if((response.result.value) != nil) {
} else {
}
}
case .failure( _):
break
}
}
)
For me the build error was caused by a multipartFormData.appendBodyData(). After replacing it with multipartFormData.append() the problem was solved.
I got the same error, after spending lots of time, i found that issue was:
I was passing MutableURLRequest instead of passing URLRequest object. Thats why i were getting this error. After type casting it to URLRequest, it start working.

Upload of data not populating $_FILES

I've been experiencing some issues to upload a file to a server using Alamofire.
The files are uploaded well, but the variables where the upload should appear are empty : FILES:array(0) {\n}\nPOST:array(0) {\n}\n
And using the website, we can upload images without any problem
Would anyone have an explanation or a solution to my problem?
(the file size is not an issue, I send a 370kb image and the limit is 20mb)
func postImages(images : [UIImage], compression : CGFloat, completion: ((AnyObject?)->())? = nil) {
AlamoNoCache.manager?.upload(.POST,
"\(MYURL)/upload?access_token=" + "youwontknowit",
multipartFormData: {
multipartFormData in
for (index, image) in images.enumerate() {
multipartFormData.appendBodyPart(data: UIImageJPEGRepresentation(image, compression)!, name: "file", mimeType: "image/jpeg")
}
}, encodingCompletion: {
encodingResult in
switch encodingResult {
case .Success(let upload, let streamingFromDisk, let streamFileURL):
print(upload)
print(streamingFromDisk)
print(streamFileURL)
upload
.progress({
a, b, c in
print("\(a), \(b), \(c)")
})
.responseString(completionHandler: {
a in
print("\(a)")
}
)
break
case .Failure(let error):
print(error)
break
}
})
}
Thank you for your time.
I used this with Alamofire for upload any kind of files:
let request = alamofireManager.upload(.POST, remoteURL, headers: httpHeaders, file: fileUrl);
and if you want to handle the response:
request.response {(request, response, object, error) -> Void in
...
}