Swift 3 - Alamofilre 4.0 multipart image upload with progress
Finally got the solution after search a lot. We just need to put uploadProgress block within result block.
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
}
}, to:"http://server1/upload_img.php")
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (Progress) in
print("Upload Progress: \(Progress.fractionCompleted)")
})
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)
}
}
How can I observe uploadProgress while Uploading Multipart Form Data?
Have you tried with this:
Alamofire.upload(
multipartFormData: { multipartFormData in
//your implementation
},
to: "http://example.com",
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
print(response)
}
upload.uploadProgress { progress in
print(progress.fractionCompleted)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
How I can add progress bar with label with percentage of progress of upload in Alamofire 4.0
To quote directly from the AlamoFire docs:
Upload Progress
While your user is waiting for their upload to complete, sometimes it
can be handy to show the progress of the upload to the user. Any
UploadRequest can report both upload progress and download progress of
the response data using the uploadProgress and downloadProgress APIs.
let fileURL = Bundle.main.url(forResource: "video", withExtension: "mov")
Alamofire.upload(fileURL, to: "https://httpbin.org/post")
.uploadProgress { progress in // main queue by default
print("Upload Progress: \(progress.fractionCompleted)")
}
.downloadProgress { progress in // main queue by default
print("Download Progress: \(progress.fractionCompleted)")
}
.responseJSON { response in
debugPrint(response)
}
Alamofire 4.0 Upload MultipartFormData Header
I got the solution.
Alamofire.upload(multipartFormData:{ multipartFormData in
multipartFormData.append(unicornImageURL, withName: "unicorn")
multipartFormData.append(rainbowImageURL, withName: "rainbow")},
usingThreshold:UInt64.init(),
to:"https://httpbin.org/post",
method:.post,
headers:["Authorization": "auth_token"],
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})
Hope it will help you.
Upload Multipart swift Image Upload internal server error 500 in response
There is a difference in this line:
multipartFormData.append("\(UserManager._currentUser?.userID)".data(using: .utf8, allowLossyConversion: false)!, withName: "id")`.
You don't force unwrap the UserManager._currentUser?.userID
like you do in the 2.3 version:
multipartFormData.appendBodyPart(data: "\((UserManager._currentUser?.userID)!)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"id")
So your string for that "id" field is probably something like "Optional("userID")" instead of just the user ID you're expecting.
Related Topics
Add Uitapgesturerecognizer to Uitextview Without Blocking Textview Touches
Uibutton with Single Press and Long Press Events Swift
Why Is Audio Coming Up Garbled When Using Avassetreader with Audio Queue
Com.Facebook.Sdk Error 2 on iOS
How to Call Https Url in Uiwebview
The 'Pods' Target Has Transitive Dependencies That Include Static Binaries' When Installing Gcm
Calling a Method at Specific Time Every Day
Uitableviewcell Textlabel Color Not Changing
API Facebook iPhone , Possible to Post to a Friend's Wall
iPhone Camera, How to Avoid Cameraoverlay on Preivew View; How to Know When Entering Preview View
Get Cellid, Mcc, Mnc, Lac, Signal Strength, Quality and Network in iOS 8.3
Locking Orientation Does Not Work on iPad iOS 8 Swift Xcode 6.2
How to Open a Link to a PDF with Wkwebview