OGeek|极客世界-中国程序员成长平台

标题: ios - AFNetworking 上传使用 forKey 引用的文件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:05
标题: ios - AFNetworking 上传使用 forKey 引用的文件

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

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

非常感谢!



Best Answer-推荐答案


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

NSMutableURLRequest *request = [[AFHTTPClient sharedClient] multipartFormRequestWithMethod"OST" 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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4