AFNetworking上传用forKey引用的文件

时间:2011-11-04 21:34:38

标签: ios url post file-upload afnetworking

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

我使用的是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我不知道应该写什么,这些例子对我没有帮助

非常感谢你!

1 个答案:

答案 0 :(得分:4)

你需要使用" multipartFormRequestWithMethod" - 例如:

NSMutableURLRequest *request = [[AFHTTPClient sharedClient] multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
  [formData appendPartWithFileData:data mimeType:@"image/jpeg" name:@"attachment"];
}];