使用AFNetworking 2.0上传图像 [英] Uploading image with AFNetworking 2.0

查看:266
本文介绍了使用AFNetworking 2.0上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个我正在撞墙。我想从库中选择 UIImage ,并将其上传到服务器,就像< form action =http://blabla.request .cfmmethod =postenctype =multipart / form-data> 。而不是成功,我得到这个错误:

lockquote

错误=错误域= NSCocoaErrorDomain代码= 3840操作不能完成(Cocoa )(JSON文本没有开始数组或对象和选项,以允许片段不设置。)UserInfo = 0x145e5d90 {NSDebugDescription = JSON文本没有开始数组或对象和选项,以允许片段没有设置。

我试过这种方法:

  - (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 constructBodyWithBlock:^(id< AFMultipartFormData> formData){
if(self.imageView.image){
[formData appendPartWithFileData:imageData name:@avatar fileName:@avatar.jpgmimeType:@image / jpeg];

}成功:^(AFHTTPRequestOperation * operation,id responseObject){
NSLog(@[UploadVC] success =%@,responseObject);
}失败:^(AFHTTPRequestOperation * operation,NSError * error){
NSLog(@[UploadVC] error =%@,error);
}];

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

但它不工作...服务器说没有文件。不知道是否加密是错误的,MIME类型或什么?

试了这个:

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

与此:

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

没有任何工作。希望有人可以帮助,因为我坚持它,并从问题到问题在这里盘旋
tia



编辑新尝试

1)首先是多部分表单

2)创建上传任务

他们都没有为我工作,所以我仍然试图解决这个问题,但是看不到任何解决方案。

我认为有些细节是缺失的)是负责任的,但我终于做到了:)在这里你去:

   - (void) uploadPhoto {
AFHTTPRequestOperationManager * manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@http://server.url]];
NSData * imageData = UIImageJPEGRepresentation(self.avatarView.image,0.5);
NSDictionary *参数= @ {@username:self.username,@password:self.password};
AFHTTPRequestOperation * op = [manager POST:@rest.of.urlparameters:parameters constructBodyWithBlock:^(id< AFMultipartFormData> formData){
//不要像我这样做,但附加它!
[formData appendPartWithFileData:imageData name:paramNameForImage fileName:@photo.jpgmimeType:@image / jpeg];
}成功:^(AFHTTPRequestOperation * operation,id responseObject){
NSLog(@Success:%@ *****%@,operation.responseString,responseObject);
}失败:^(AFHTTPRequestOperation * operation,NSError * error){
NSLog(@Error:%@ *****%@,operation.responseString,error);
}];
[op start];

$ / code $ / pre
$ b $ p

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

解决方案

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

这篇关于使用AFNetworking 2.0上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆