AFNetworking form 表单上传图片的问题
2016-12-19 本文已影响0人
字母大师
afnetworking 上传图片,后台接受不到参数,无奈之下,参数拼在了url之后,图片成功上传 ,首先给有同样困惑的人一个解决方法,同是求问,放在请求体参数中为什么不行??
下面上代码
//处理拼接后的特殊字符处理
NSString *encodedString = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)jsonstring, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));
NSString *str = [NSString stringWithFormat:@"%@?auth=%@&token=%@",exChangeHeadeImage,encodedString,token];
AFHTTPSessionManager *manager = [Manager sharedHTTPSession];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
@"text/html",
@"text/plain",
@"image/jpeg",
@"image/png",
@"application/octet-stream",
@"text/json",
nil];
[manager POST:str parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.3) name:@"file" fileName:@"123.jpg" mimeType:@"image/jpg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
KKLog(@"%@",dict);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];