AFnetworking以base64编码格式的图像的多部分数据

时间:2016-05-18 10:32:52

标签: ios objective-c base64 image-uploading afnetworking-2

如何使用af网络将Base64映像编码发送到服务器。我已将图像转换为base 64但问题在于将图像发送到服务器。我是ios的新手,所以请帮我解决这个问题。

NSString *surl = @"https://xxxxxxxxxxxxx";
            surl = [surl stringByAppendingString:userID];
            NSLog(@"%@", surl);

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:[NSURL URLWithString:surl]];
            [request setHTTPMethod:@"POST"];
            [request setValue:@"multipart/form-data" forHTTPHeaderField:@"Accept"];




            NSMutableData *postBody = [NSMutableData data];
            [postBody appendData:[base64 dataUsingEncoding:NSUTF8StringEncoding]];


            [request setHTTPBody:postBody];
            NSLog(@"postbody%@", postBody);
            AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

            operation.responseSerializer = [AFHTTPResponseSerializer serializer];

            [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSLog(@"JSON Successsss: %@", responseObject);


            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"error: %@",error);
                NSHTTPURLResponse *response = (NSHTTPURLResponse *)operation.response;

                NSLog(@"statusCode: %ld", (long)response.statusCode);

                NSString* ErrorResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
                NSLog(@"Error Response:%@",ErrorResponse);

            }];
            [[NSOperationQueue mainQueue] addOperation:operation];
        }

0 个答案:

没有答案