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
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…