2014-03-04 145 views
0

我試圖將圖片上傳到服務器,但我遇到了問題。下面是我的代碼:AFNetworking上傳圖片錯誤

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,但服務器沒有更新。我究竟做錯了什麼?

編輯

好,圖像被正確上傳,但是我們不得不使用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); 
     }]; 
0

固定!通過POST調用,我需要設置正確的參數:@{user_id:@6}。現在工作很好!