Add progress to file uploading using Alamofire
you can do like this:
Alamofire.upload(
.POST,
URLString: "http://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.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
let progress: Float = Float(totalBytesRead)/Float(totalBytesExpectedToRead) // you can give this progress to progressbar progress
let value = Int(progress * 100) // this is the percentage of the video uploading
print(totalBytesRead)
}
upload.responseJSON { request, response, result in
debugPrint(result)
}
case .Failure(let encodingError):
print(encodingError)
}
}
)
How to detect upload progress and upload status in Alamofire 5 with Swift?
try this :-
AF.upload(multipartFormData: { MultipartFormData in
MultipartFormData.append(fileContent, withName: "file" , fileName: filePath.lastPathComponent , mimeType: "image/jpeg")
for(key,value) in dictonary {
MultipartFormData.append(token.data(using: String.Encoding.utf8)!, withName: "token")
}
}, to: uploadURL, method: .post, headers: ["Content-Type": "application/json")
.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
.responseJSON{ (response) in
debugPrint("SUCCESS RESPONSE: \(response)")
}
}
Alamofire - how to have progress and completion closure with multipart upload
Here is a way to have completion, failure and progress closures (thanks to my colleague for point me to the solution):
Alamofire.upload(.POST, absPath(), headers: headers(), multipartFormData: { (multipartFormData:MultipartFormData) -> Void in
multipartFormData.appendBodyPart(data: json, name: "metadata", mimeType: "application/json")
multipartFormData.appendBodyPart(data: self.data, name: "document", fileName: "photo.png", mimeType: "image/png")
}, encodingMemoryThreshold: 10 * 1024 * 1024, encodingCompletion: { (encodingResult) -> Void in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
// success block
}
upload.progress { _, totalBytesRead, totalBytesExpectedToRead in
let progress = Float(totalBytesRead)/Float(totalBytesExpectedToRead)
// progress block
}
case .Failure(_):
// failure block
}
})
Related Topics
Playing an Audio File Repeatedly with Avaudioengine
How to Handle Oauth-Style Log Ins with Imessage Apps in iOS 10, Xcode 8
Please Specify a Platform for This Target in Your Podfile
Swift Select All Photos from Specific Photos Album
Using Haneke to Cache Then Play Mp4 Files with Avplayer
Supporting a Nsmanagedobject Fetchrequest() Class Method in iOS 9 and 10
Import Xctest into a Dynamic Framework
How to Draw Geojson in Apple Maps as Overlay Using Swift 3
Why am I Getting an Error Regarding Bolts Framework and Facebooksdk When I'm Not Even Using Bolts
Get All List of Uiviewcontrollers in iOS Swift
Gmsmarker Icon in the Top Left Corner of the View (Ios)
Save Depth Images from Truedepth Camera
How to Change Searchbar Border Color
How to Get User Data from Facebook Sdk on iOS