采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!

iOS 码拜 10年前 (2015-05-02) 955次浏览 0个评论

采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,两种写法如下,求教,能给个思路吗?

IOS AFNetworking upload pictures to the server.The request is successful,But it can not parse base64 image.Then same request using ASIHTTPReuqest framework upload base64 images on the server side can parse is successful.Can you give a suggestion or idea?

AFNworking request Code:

    UIImage *img = [avatar imageWithImageSimple:avatar scaledToSize:EZSIZE(320, 320)];

    NSData* imageData = UIImageJPEGRepresentation(img, 0.5);

    NSString *strBase64 = [imageData base64String];

    NSDictionary * object = @{
               @”firstname”  :@”nameTest”,
               @”sex”        :@”male”,
               @”user_id”    :@”1234556″,
               @”avatar”     :strBase64,
               @”phone”      :@”15899402939″
               };
   NSMutableDictionary *parameter = [[NSMutableDictionary alloc] init];

   [parameter setObject:@1 forKey:@”app”];

   [parameter setObject:@”save_settings” forKey:@”m_action”];

   [parameter setObject:[[object JSONRepresentation] forKey:@”m_param”]];
    
    NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@”POST”
                                                                                       URLString:@”http://test…”
                                                                                      parameters:parameter
                                                                       constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                                       } error:nil];

   
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

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

    }];

ASIHttpRequest request code:

    UIImage *img = [avatar imageWithImageSimple:avatar scaledToSize:EZSIZE(320, 320)];
    NSData* imageData = UIImageJPEGRepresentation(img, 0.5);
    NSString *strBase64 = [imageData base64String];
    
    NSDictionary * object = @{
                              @”firstname”  :@”nameTest”,
                              @”sex”        :@”male”,
                              @”user_id”    :@”1234556″,
                              @”avatar”     :strBase64,
                              @”phone”      :@”15899402939″
                              };
    
    NSString *parameter = @”app=1″;

    parameter = [[parameter stringByAppendingString:@”&m_action=”] stringByAppendingString:@”save_settings”];

    parameter = [[parameter stringByAppendingString:@”&m_param=”] stringByAppendingString:[object JSONRepresentation]];

    NSData *data = [parameter dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    
    [request setDidFailSelector:@selector(handleRequestEventError:)];
    [request setDidFinishSelector:@selector(parseAndSave:)];
    
    [request addRequestHeader:@”Content-Length” value:[@([data length]) stringValue]];
    [request setRequestMethod:@”POST”];
    [request appendPostData:data];
    [request addRequestHeader:@”Content-Type” value:@”application/x-www-form-urlencoded”];
    
    [request setDelegate:self];
    
    
    [_networkQueue addOperation:request];
    

采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!
你居然把stackoverflow上的帖子直接copy过来
太不仗义了
采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!
stackoverflow 也是我发的。。github 中 AFNetwoking 提的问题中,我也发过。
采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!
真没办法,找不到解决的问题。
采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!
如果base64解析不了的话你可以直接传图片上去让服务器端处理啊,我直接贴上我传图片的代码,base64的我也有用过,没什么问题啊
+(AFHTTPRequestOperation *)updateCover:(short)coverIndex
                              imgCover:(UIImage *)imgCover
                                aBlock:(resultBlock)aBlock;
{
    NSDictionary *param=@{@”token”:[publicTools getIdToken],
                          @”id”:[@([currUser shareInstance].userID) stringValue],
                          @”action”:@”updatestorycover”,
                          @”cover_index”:[@(coverIndex) stringValue]
                          };
    
    return [[netEngine uploadEngine] POST:XXX parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:UIImageJPEGRepresentation(imgCover, .5) name:@”uploadimg” fileName:@”asdf.jpeg” mimeType:@”image/jpeg”];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        aBlock((NSDictionary *)responseObject,
               [[(NSDictionary *)responseObject valueForKey:@”success”] intValue],
               [(NSDictionary *)responseObject valueForKey:@”data”],
               [(NSDictionary *)responseObject valueForKey:@”message”]
               );
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self processError:error aBlock:aBlock];
    }];
}
采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!
你把AFNetworking和ASI对同一图片的base64分别发出来,我试试能不能解析
采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!
ASI 请求参数:
app=x&m_action=save_settings1&m_param={“firstname”:”xxx”,”sex”:1,”user_id”:311xxx,”phone”:”xxxxx”,”avatar”:“/9j/4AAQSkZJRgABAQEASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAABQKADAAQAAAABAAABQAAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAxDW0HYxmixn0ozSA04VZAzmmOwAqQ1C+………………………MUmwSP/9XjUuGkiMU0YUw+Wdx5HPXFNn1eeLUJYnwyyxhI+uMHgn86zdWuR8k8c/XiukutQg1NftF5DkEkEjt+WK89hHmN5YOMdP512dm5ljaGJCw4JHp2zXNiYxjqkTdJaoSwWy0vVLdpBvX5mX8cgCvQYIIuM00mjr9pCSbTPGr7S57O4/dr8j8r/AIVRVpLd8ocMOte4Dwl4nRI5UsJs8ZGBwc896pa78ONbuI/7RtLGUSYO9OOeOMVtTxErqM4nHUox1lBn/9k=“}
==============================================================================================
AFN  base64:
{
    app = X;
    “m_action” = “save_settings1”;
    “m_param” = “{“firstname”:”xxxx”,”sex”:1,”user_id\”:12323,\”phone\”:\”32132132\”,\”avatar”:”/9j/4AAQSkZJRgABAQEASABIAAD/4QBYRXhpZgAATU0AKgAA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8ZGBwc896pa78ONbuI/7RtLGUSYO9OOeOMVtTxErqM4nHUox1lBn/9k=”}”;
因为base64太长了,超出输入框字数了。两个请求上传的base64是一样的。区别是AFN开始和结尾的时候多了一个转义符“\”。
采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!
– (void)testDicty
{
    NSDictionary *dic = @{@”name1″ :@”3432″,
                          @”name1″ :@”3432″,
                          @”name1″ :@”3432″
                          };
    NSDictionary *dicy = @{@”name” :@”zhangsan”,
                           @”age”  :@”12″,
                           @”param” : [dic JSONRepresentation]
                           };
    NSLog(@”dicy = %@”,dicy);
}
打印输出:
2015-03-19 10:39:51.265 SD4iPad[1926:54805] dicy = {
    age = 12;
    name = zhangsan;
    param = “{“name1″:”3432″}”;
}
是不是因为当一个字典里包含有一个[dic JSONRepresentation]对象,然后dic里的Json数据就会有转义字符? 是不是这个照成的?求解。
采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!
40分
引用 7 楼 LmyangBK 的回复:

– (void)testDicty
{
    NSDictionary *dic = @{@”name1″ :@”3432″,
                          @”name1″ :@”3432″,
                          @”name1″ :@”3432″
                          };
    NSDictionary *dicy = @{@”name” :@”zhangsan”,
                           @”age”  :@”12″,
                           @”param” : [dic JSONRepresentation]
                           };
    NSLog(@”dicy = %@”,dicy);
}
打印输出:
2015-03-19 10:39:51.265 SD4iPad[1926:54805] dicy = {
    age = 12;
    name = zhangsan;
    param = “{“name1″:”3432″}”;
}
是不是因为当一个字典里包含有一个[dic JSONRepresentation]对象,然后dic里的Json数据就会有转义字符? 是不是这个照成的?求解。

这样有影响,如果json里套json的话,服务器拿到param后,实际上拿到的是一个字符串,需要再次把这个字符串解析为json


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明采用AFNetWorking 上传base64的图片到服务器,上传的图片解析不了。ASIHttpRequest上传是可以的,请教!
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!