Ios AFN上载用forKey引用的文件

Ios AFN上载用forKey引用的文件,ios,url,post,file-upload,afnetworking,Ios,Url,Post,File Upload,Afnetworking,我需要向web服务器发送一些用户在我的应用程序中插入的信息 我使用的是ASIHTTPRequest,但我在iOS5上遇到了一些问题,所以我决定改用AftRequest 这是我到目前为止写的代码 AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://theserver.com/upload.php"]]; NSMutableDictionary *parameters = [NSMut

我需要向web服务器发送一些用户在我的应用程序中插入的信息

我使用的是ASIHTTPRequest,但我在iOS5上遇到了一些问题,所以我决定改用AftRequest

这是我到目前为止写的代码

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”引用

我应该写什么?我用了一个HttpRequest

[request setData:myNSData withFileName:@"image.jpg" andContentType:@"application/octet-stream" forKey:@"attachment"];
但对于AFNetworking,我不知道该写什么,而且这些例子也帮不了我

多谢各位

您需要使用“multipartFormRequestWithMethod”-例如:

NSMutableURLRequest*request=[[AFHTTPClient sharedClient]multipartFormRequestWithMethod:@“POST”路径:@”/upload.php“参数:nil constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:data mimeType:@“图像/jpeg”名称:@“附件”];
}];

谢谢!!但是我应该将我的“NSMutableDictionary*参数”传递给它,而不是“parameters=nil”,对吗?当我声明“AFHTTPClient*client”。。。我需要插入整个路径或仅插入“”?是的,您可以传入参数。创建AFHTTPClient时,只需传递基本url-ie(使用http://,stackoverflow似乎有意隐藏该url)数据是UIImage吗?这有关系吗?@mkral data是一个NSData。要将UIImage转换为NSData,请查看UIImageJPEGRepresentation()。
NSMutableURLRequest *request = [[AFHTTPClient sharedClient] multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
  [formData appendPartWithFileData:data mimeType:@"image/jpeg" name:@"attachment"];
}];