Uploading image with AFNetworking 2.0
Asked Answered
H

3

33

I'm banging my head against the wall with this one. I want to select UIImage from library and upload it to server, like on could do with <form action="http://blabla.request.cfm" method="post" enctype="multipart/form-data">. Instead of success I got this error:

error = Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x145e5d90 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

I tried this way:

-(void)uploadPhoto{
NSString *path = @"http://blabla.request.cfm";
NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 0.9);
int priv = self.isPrivate ? 1 : 0;
NSDictionary *parameters = @{@"username": self.username, @"password" : self.password, @"private" : @(priv), @"photo" : imageData};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    if(self.imageView.image){
        [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
    }
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"[UploadVC] success = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"[UploadVC] error = %@", error);
}];

[self blockView:self.view block:YES];
}

but it's not working... server says that there is no file. Not sure if encrypting is wrong, mime type or what?

Tried also this:

    [manager POST:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"[UploadVC] success = %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"[UploadVC] error = %@", error);
}];

and this:

    manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:path parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFormData:imageData name:@"photo"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"[UploadVC] success = %@", responseObject);
    [self blockView:self.view block:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"[UploadVC] error response.object = %@", operation.responseObject);
    [self blockView:self.view block:NO];
}];

nothing is working. Hope someone can help, 'cause I'm stuck with it and circling from question to question here on SO
tia

EDIT: new attempt
1) first was multi-part form
2) creating upload task
none of them worked for me, so I'm still trying to cope with that, but cannot see any solution

Hereditary answered 7/11, 2013 at 12:49 Comment(4)
What is the log of [operation responseString] on failure block ?Excommunication
you mean from the first example? it is html file from server with table containing info on error (from coldfusion)Hereditary
actually it is really messy, but from it I can find that section Diagnostic says: The form field photo did not contain a fileHereditary
I don't know whats wrong in this parameter [formData appendPartWithFileData:imageData name:@"public.image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];Gothart
H
94

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<AFMultipartFormData> 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 :)

Hereditary answered 7/11, 2013 at 16:57 Comment(7)
rest of server url, notice that base url is in AFHTTPRequestOperationManager init methodHereditary
Whats the need for "[op start];" ? I did this in the same way as above but link returns success, but file not uploaded to the server.Tarrah
[op start]; -->to start uploading.Invoice
@Hereditary Hey, this code works for me, but same code with session manager doesn't work. Maybe you have an idea why this happens?Maryjanemaryjo
what is name:paramNameForImage can u please help meGothart
I don't know whats wrong in this parameter [formData appendPartWithFileData:imageData name:@"public.image" fileName:@"photo.jpg" mimeType:@"image/jpeg"];Gothart
Is there a way to do it that isn't loading a file into memory?Prenomen
A
2

You can upload an image with AFNetworking using Swift like this...

    let compression = 0.5

    let imageData = UIImageJPEGRepresentation("image", CGFloat(compression))

if imageData != nil{
   var manager = AFHTTPRequestOperationManager()
        manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/html", "application/json"]) as Set<NSObject>

        var dictParams = [
        "familyId":"10000",
        "contentBody" : "Some body content for the test application",
        "name" : "the name/title",
        "typeOfContent":"photo"
    ]

        let url = "http://...."
        manager.POST(url, parameters: dictParams, constructingBodyWithBlock: { (formData: AFMultipartFormData!) -> Void in
            //code
            formData.appendPartWithFileData(imageData, name: "file", fileName: "filename", mimeType: "image/png")
            }, success: { (operation:AFHTTPRequestOperation!, responseObject:AnyObject!) -> Void in
                println(responseObject)
            }, failure: { (operation:AFHTTPRequestOperation!, error:NSError!) -> Void in
                println(error)
        })
    }
Alphonsealphonsine answered 16/5, 2015 at 13:32 Comment(0)
O
1
 UIImage *image = [UIImage imageNamed:@"decline_clicked.png"];
NSData *imageData = UIImageJPEGRepresentation(image,1);


NSString *queryStringss = [NSString stringWithFormat:@"http://your server/uploadfile/"];
queryStringss = [queryStringss stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];


[manager POST:queryStringss parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
 {


     [formData appendPartWithFileData:imageData name:@"fileName" fileName:@"decline_clicked.png" mimeType:@"image/jpeg"];



 }
      success:^(AFHTTPRequestOperation *operation, id responseObject)
 {



    NSDictionary *dict = [responseObject objectForKey:@"Result"];

    NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);


 }
      failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {

     NSLog(@"Error: %@ ***** %@", operation.responseString, error);
 }];
Obvert answered 18/8, 2014 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.