Add Progress to File Uploading Using Alamofire

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



Leave a reply



Submit