Afnetworking 2.2.0 Upload Image on Server Issues

AFnetworking 2.2.0 upload image on server issues

Have you confirmed that the file is found at that location in your Documents folder? I'm also having trouble reconciling your programmatically determined file path (which is correct) with your string literal file path (which can easily be problematic). You should always programmatically determine the path, not using string literals (because when you reinstall the app, that path will change). What I think you need is something like:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];

[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSError *error;
BOOL success = [formData appendPartWithFileURL:fileURL name:@"image" fileName:filePath mimeType:@"image/png" error:&error];
if (!success)
NSLog(@"appendPartWithFileURL error: %@", error);
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

Note, this programmatically determines the filePath (which is a path, not a URL), and then uses fileURLWithPath to convert that to a file URL. It also confirms whether it was successful or not, logging the error if not successful.

Also note that this assumes your server is returning its response as JSON. If not, you'd have to change the responseSerializer.

Error when uploading photo with AFNetworking

I finally found a solution in this answer:
https://stackoverflow.com/a/21304062/569507

However, in stead of subclassing the AFHTTPRequestOperation as suggested, I simply made a category on it (or actually on its parent class AFURLConnectionOperation which implements the NSURLConnectionDataDelegate protocol) that contains the connection:needNewBodyStream method. This is all that is needed. The rest of my code remains unaltered.

AFURLConnectionOperation+Recover.h

#import "AFURLConnectionOperation.h"

@interface AFURLConnectionOperation (Recover)

- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request;

@end

AFURLConnectionOperation+Recover.m

#import "AFURLConnectionOperation+Recover.h"

@implementation AFURLConnectionOperation (Recover)

- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request
{
if ([request.HTTPBodyStream conformsToProtocol:@protocol(NSCopying)]) {
return [request.HTTPBodyStream copy];
}
return nil;
}

@end

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:^(id<AFMultipartFormData>formData){
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 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:^(id<AFMultipartFormData>formData){
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.

Uploading images using AFNetworking

Try this.

-(void)uploadImage1:(UIImage *)img Dictionary:(NSMutableDictionary *)dictParam {
NSData *imageData;
if (img == nil) {

}
else {
imageData = UIImageJPEGRepresentation(img, 0.7); // IF you want to compress image
}

AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];

requestSerializer = [AFJSONRequestSerializer serializer];
responseSerializer = [AFJSONResponseSerializer serializer];

NSError *__autoreleasing* error = NULL;

NSMutableURLRequest *request = [requestSerializer multipartFormRequestWithMethod:@"POST" URLString:"YOUR_URL" parameters:dictParam constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (imageData != nil) {
[formData appendPartWithFileData:imageData
name:@"profile_image" // Name of your Image Param
fileName:@"user_image.jpg"
mimeType:@"image/jpg"];
}
} error:(NSError *__autoreleasing *)error];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

manager.responseSerializer = responseSerializer;

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {

}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
[SVProgressHUD dismiss];
NSLog(@"ERROR = %@",error.localizedDescription);
}
else {
if ([[responseObject valueForKey:@"status"] intValue] == 1) {
NSLog(@"SUCCESS");
}
else {
NSLog(@"ERROR FROM SERVER = %@", [responseObject valueForKey:@"message"]);
}
}
}];

[uploadTask resume];
}


Related Topics



Leave a reply



Submit