我需要将用户在我的应用程序中插入的一些信息发送到网络服务器
我使用的是 ASIHTTPRequest,但在 iOS 5 上遇到了一些问题,所以我决定转向 AFNetworking
这是我到目前为止编写的代码...
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://theserver.com/upload.php"]];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:[fieldName text] forKey:@"name"];
[parameters setObject:[fieldSurname text] forKey:@"surname"];
现在我需要添加一个包含图像的 NSData 对象,并且应该通过 forKey:@"attachment"引用它
我应该写什么?与我正在使用的 ASIHTTPRequest
[request setData:myNSData withFileName:@"image.jpg" andContentType:@"application/octet-stream" forKey:@"attachment"];
但是对于 AFNetworking,我不知道应该写什么,而且这些示例对我没有帮助
非常感谢!
最佳答案
您需要使用“multipartFormRequestWithMethod” - 例如:
NSMutableURLRequest *request = [[AFHTTPClient sharedClient] multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:data mimeType:@"image/jpeg" name:@"attachment"];
}];
关于ios - AFNetworking 上传使用 forKey 引用的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8016004/