Loading an image using AFNetworking with a null parameter does not work

I want to upload the image to the server too, but got an error while doing this

I used the following code, but I don’t know what is wrong there.

+(void) HTTPPostImage:(NSString *) stringURL andParameter:(NSData *) imageData andSelector:(SEL) selector andTarget:(id) target{ AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:stringURL]]; // NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5); NSDictionary *parameters =NULL; NSString *myUUID = [[NSUUID UUID] UUIDString]; // create a UUID NSLog(@"myUUID%@",myUUID); AFHTTPRequestOperation *op = [manager POST:@"" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"public.image" fileName:@"photo.jpg" mimeType:@"image/jpeg"]; //[formData appendPartWithFileData:imageData name:@"file" fileName:myUUID 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]; } 

I do not know what is wrong with this parameter

 [formData appendPartWithFileData:imageData name:@"public.image" fileName:@"photo.jpg" mimeType:@"image/jpeg"]; 

For server parameter

I don’t know if I made mistakes or the server did it

enter image description here

0
ios objective-c afnetworking-2
Oct. 20 '15 at 5:44
source share
2 answers

Add your last URL component name and change your code as follows:

 AFHTTPRequestOperation *op = [manager POST:@"suburl.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"photo.jpg" mimeType:@"image/jpeg"]; //[formData appendPartWithFileData:imageData name:@"file" fileName:myUUID mimeType:@"image/jpeg"]; 



 AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://yourbaseurl.com/"]]; // NSData *imageData = UIImageJPEGRepresentation(self.avatarView.image, 0.5); NSDictionary *parameters =NULL; NSString *myUUID = [[NSUUID UUID] UUIDString]; // create a UUID NSLog(@"myUUID%@",myUUID); AFHTTPRequestOperation *op = [manager POST:@"suburl.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"photo.jpg" mimeType:@"image/jpeg"]; //[formData appendPartWithFileData:imageData name:@"file" fileName:myUUID 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]; 
0
Oct 20 '15 at 6:00
source share

Please try entering the code below. Hope for his work for you.

  NSData *tempData = [NSData dataWithContentsOfFile:YOUR_IMAGE_FILE_PATH]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; [manager POST:YOUR_REQUEST_URL parameters:YOUR_PARAMETERS constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { if (tempData) { [formData appendPartWithFileData:tempData name:@"user_image" fileName:@"avatar.jpeg" mimeType:@"image/jpeg"]; } } success:^(AFHTTPRequestOperation *operation, id responseObject){ NSError *error; NSDictionary* json = [NSJSONSerialization JSONObjectWithData:operation.responseData options:kNilOptions error:&error]; NSLog(@"Registration JSON:- %@",json); }failure:^(AFHTTPRequestOperation *operation, NSError *error){ NSLog(@"Error: %@", error); }]; 

"user_image" is the key parameter for the image. This key is set in the API request parameter.

Thank:)

0
Oct. 20 '15 at 6:23
source share



All Articles