Created
February 17, 2016 05:25
-
-
Save hide1202/5b5cb32c2778eb24c4e0 to your computer and use it in GitHub Desktop.
AFNetworking.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { | |
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil]; | |
} error:nil]; | |
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; | |
NSURLSessionUploadTask *uploadTask; | |
uploadTask = [manager | |
uploadTaskWithStreamedRequest:request | |
progress:^(NSProgress * _Nonnull uploadProgress) { | |
// This is not called back on the main queue. | |
// You are responsible for dispatching to the main queue for UI updates | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
//Update the progress view | |
[progressView setProgress:uploadProgress.fractionCompleted]; | |
}); | |
} | |
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { | |
if (error) { | |
NSLog(@"Error: %@", error); | |
} else { | |
NSLog(@"%@ %@", response, responseObject); | |
} | |
}]; | |
[uploadTask resume]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment