我正在尝试将图片上传到服务器,但我遇到了问题。这是我的代码:
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来检索图片。为什么会这样?
答案 0 :(得分:0)
试试这个
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);
}];
答案 1 :(得分:0)
固定!通过POST调用,我需要设置正确的参数:@{user_id:@6}
。现在工作得很好!