Uploading Image with Afnetworking 2.0

Uploading image with AFNetworking 2.0

I'm not sure which part (I think that some details were missing) was responsible, but I did it finally :) here you go:

-(void)uploadPhoto{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://server.url"]];
NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5);
NSDictionary *parameters = @{@"username": self.username, @"password" : self.password};
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];
}

Works like a charm :)

iOS Image upload via AFNetworking 2.0

I ended up using the multi-part request

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id formData) {
[formData appendPartWithFormData:imageData name:@"image"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

Upload multiple Image or File using AFNetworking,

Check this method from AFURLSessionManager:

- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request
fromFile:(NSURL *)fileURL
progress:(void (^)(NSProgress *uploadProgress)) uploadProgressBlock
completionHandler:(void (^)(NSURLResponse *response, id responseObject, NSError *error))completionHandler

Full code implementation:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *sessionManager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

[[sessionManager uploadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@BaseURL(@"/MediaUpload")]]
fromFile:[NSURL fileURLWithPath:@"/path/to/uploading_file"]
progress:^(NSProgress * _Nonnull uploadProgress) { }
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
}] resume];

Afnetworking 2.0 PUT image

You are not uploading an image; but rather a URL of the image (note the url parameter).

Therefore you will need to upload the image to a 3rd party site and then post the link to whatever that service is.

It's impossible to upload an image using a PUT request so you must be missing something.

Upload multiple images using AFNetworking

UIImage *image1 = [UIImage imageNamed:@"about_app"];
UIImage *image2 = [UIImage imageNamed:@"alter"];
NSArray *array = @[image1,image2];
NSMutableURLRequest *request = [[AFNetWorkSingleton shareInstance] multipartFormRequestWithMethod:@"POST" path:@"Mindex/getimg" parameters:nil constructingBodyWithBlock:^(idformData){
int i = 0;
for(UIImage *eachImage in array)
{
NSData *imageData = UIImageJPEGRepresentation(eachImage,0.5);
[formData appendPartWithFileData:imageData name:[NSString stringWithFormat:@"file%d",i ] fileName:[NSString stringWithFormat:@"file%d.jpg",i ] mimeType:@"image/jpeg"];
i++;
}
}];

Try this.

Upload Image and Information (both) Using AFNetworking 2.0 in Single PHP

the issue seems to be on the PHP side
the lines

$json = file_get_contents('php://input'); // Catching input
$value= json_decode($json, true); // Decode JSON into Dictionary.

do not work. Replacing the above block with this line

$value= $_REQUEST;

should resolve your issue .



Related Topics



Leave a reply



Submit