AFNetworking 上传图片错误
Posted
技术标签:
【中文标题】AFNetworking 上传图片错误【英文标题】:AFNetworking Upload Image Error 【发布时间】:2014-03-04 05:14:40 【问题描述】:我正在尝试将图像上传到服务器,但遇到了问题。这是我的代码:
UIImage *image = [UIImage imageNamed:@"profileImage.png"];
NSData *imageData = UIImagePNGRepresentation(image);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://myserver.com/upload_img.php?&user_id=6&image=image.png" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
[formData appendPartWithFileData:imageData name:@"image" fileName:@"image.png" mimeType:@"image/png"];
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"Success: %@", responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
我从 NSLog 收到“Success: error = "";",但服务器没有更新。我做错了什么?
编辑
好的,图片已正确上传,但我们必须在服务器上使用 GET 而不是 POST 来检索图片。为什么会这样?
【问题讨论】:
我猜你需要调试服务器才能弄清楚发生了什么。 【参考方案1】:试试这个
UIImage *image = [UIImage imageNamed:@"profileImage.png"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://myserver.com/upload_img.php?&user_id=6&image=image.png" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
[formData appendPartWithFileData:imageData name:@"image" fileName:@"image.png" mimeType:@"image/png"];
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"Success: %@", responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
【讨论】:
【参考方案2】:已修复!通过 POST 调用,我需要设置正确的参数:@user_id:@6
。现在工作得很好!
【讨论】:
以上是关于AFNetworking 上传图片错误的主要内容,如果未能解决你的问题,请参考以下文章