如何使用AFNetworking形成一个多部分表单数据请求1.3.3
我正在使用AFNetworking
1.3.3。
我需要使用图像数据和其他一些东西来向服务器发送multipart/form-data
请求。
我不知道如何格式化这个多部分请求。 我该怎么做?
在项目的GitHub页面 (分支1.x),你可以find这个例子
NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5); NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"]; }]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); }]; [httpClient enqueueHTTPRequestOperation:operation];
您也可以阅读文档
- ASIHTTPRequest vs AFNetworking vs NSUrlRequest
- 把JSON放到一个数组中
- AFNetworking:setRedirectResponseBlock在我的类NSURLResponse总是返回空值
- 用Cocoapodspipe理多个目标的依赖关系
- Cocoapod:为什么Xcode构build过程试图find已经从我的项目中删除的库
- AFNetworking(〜> 2.5)的依赖关系不用于任何具体的目标
- AFNetworking 3 AFMultipartFormData获取字节数据作为响应
- AFURLConnectionOperation.m隐式转换失去整数精度:'int64_t'(又名'long long')到'NSInteger'(又名'int')
- 如何在AFNetworking中设置AFHTTPClient的超时时间?