0

I want too upload the image to server but got error on doing that

I have used following code but don't know whats wrong is in 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 don't know whats wrong in this parameter

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

For server parameter

I don't know whether i have done mistakes or server has done it

enter image description here

Nischal Hada
  • 3,230
  • 3
  • 27
  • 57

2 Answers2

0

Add your URL last component name and change your code like this:

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];
Nikunj
  • 655
  • 3
  • 13
  • 25
  • i got confused with name:@"userfile" fileName:@"photo.jpg" mimeType:@"image/jpeg"] what should be the parameter in these in case of user click the image – Nischal Hada Oct 20 '15 at 06:35
  • @"userfile" is type of image parameter in php service side @nischalhada – Nikunj Oct 20 '15 at 06:39
  • I have used - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info to capture image. Can u give me some ideas what will be the parameter – Nischal Hada Oct 20 '15 at 06:54
  • i don't know whether there is mistake in my side or server side – Nischal Hada Oct 21 '15 at 07:42
  • I have posted screen shots of my server info can u take a look – Nischal Hada Oct 21 '15 at 07:45
  • I have image picker then what will be the name at fileName:@"photo.jpg" can you please explain? – Suraj Sonawane Nov 27 '15 at 07:28
  • I was having a similar problem here: http://stackoverflow.com/questions/36715484/attach-image-at-url-to-afnetworking-2-0-request/36725782#36725782 – Cbas Apr 19 '16 at 17:47
0

Please try below code. Hope its 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 key parameter for a image. This key is set in API request parameter.

Thanks :)

Rupal Patel
  • 570
  • 6
  • 13