使用AFNetWorking 上传字符串,提示:Request failed: 请求太大
我要把图片换成nsdata,然后再用base64encoding,把encoding后生成的字符串上传到
服务器,就出这个问题,
报错信息:err:Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: 请求太大 (413)"
我同事用的ASIHTTPRequest库就没问题,请问如何解决?
代码:
UIImage *img = [self createThumbnailWithSourceImge:sourceImg andNewSize:sz];
data = UIImageJPEGRepresentation(img, 0.01);
NSString *encodedSTR = [data base64Encoding];
NSString *currentUrl = @"/app/b/test";
NSString *urlStr = [NSString stringWithFormat:@"%@%@?picStr=%@",BASEURL,currentUrl,encodedSTR];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc]initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSLog(@"success:%@",responseObject);
successBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failureBlock(error);
}];
[op start];
// [manager POST:urlStr parameters:nil
// success:^(AFHTTPRequestOperation *operation, id responseObject) {
// NSLog(@"success:%@",responseObject);
// } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// NSLog(@"err:%@",error);
// }];
3rd-eye
9 years, 3 months ago
Answers
图片转换成 NSData 之后就不用再转 String 了,直接上传就行了。
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString: BASEURL]];
NSData *imageData = UIImageJPEGRepresentation(img, 0.1);
NSDictionary *parameters = @{@"你们需要的参数": XXXXXXX};
AFHTTPRequestOperation *op = [manager POST:currentUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[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];
黑色餐巾纸
answered 9 years, 3 months ago
我的报404的错误 "Request failed: unacceptable content-type: text/html" 我也想知道怎么解决?
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo=0x190856b0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x17d22ca0>
Sean.
answered 9 years, 3 months ago