2013-05-30 33 views
6

我試圖用自己的API上傳圖片到ImageShack的:iOS版 - 上傳圖片與ImageShack的JSON API

- (void)uploadImage2:(UIImage *)image 
{ 
    NSData *imageToUpload = UIImagePNGRepresentation(image); 

    if (imageToUpload) 
    { 
     NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 
     [parameters setObject:@"XXXX" forKey:@"key"]; 
     [parameters setObject:@"json" forKey:@"format"]; 
     //[parameters setObject:@"application/json" forKey:@"Content-Type"]; 

     AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"https://post.imageshack.us"]]; 

     NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
      [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"logo.png" mimeType:@"image/png"]; 
     }]; 

     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
      NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; 
      NSLog(@"response: %@",jsons); 

     } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) 
     { 
      if([operation.response statusCode] == 403) 
      { 
       //NSLog(@"Upload Failed"); 
       return; 
      } 
      //NSLog(@"error: %@", [operation error]); 

     }]; 

     [operation start]; 
    } 
} 

作爲迴應,我得到錯誤信息回來,沒有錯誤的解釋:

{ 
    "error_code" = "upload_failed"; 
    "error_message" = "Upload failed"; 
    status = 0; 
} 

任何人都可以幫助我嗎?什麼是正確的方法來做到這一點?

+0

你試過發送'NSData'而不是'NSString'嗎? –

+0

是的,我做了,並得到相同的錯誤響應 – Oleg

+0

你有沒有嘗試過一個非常小的圖像? /確保它不是超時問題 – 2013-06-05 08:40:16

回答

3

好吧,我已成功地解決我的問題。所有參數都必須在表單體中設置,而不是作爲請求值。 看起來很簡單:

NSData *imageToUpload = UIImagePNGRepresentation([UIImage imageNamed:@"logo.png"]); 


    if (imageToUpload) 
    { 
     NSString *urlString = @"https://post.imageshack.us"; 

     AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlString]]; 
     NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
      [formData appendPartWithFileData:imageToUpload name:@"fileupload" fileName:@"image" mimeType:@"image/png"]; 
      [formData appendPartWithFormData:[@"XXXXXX" dataUsingEncoding:NSUTF8StringEncoding] name:@"key"]; 
      [formData appendPartWithFormData:[@"json" dataUsingEncoding:NSUTF8StringEncoding] name:@"format"]; 
     }]; 



     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
      NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; 
      NSLog(@"response: %@",jsons); 

     } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) 
     { 
      if([operation.response statusCode] == 403) 
      { 
       NSLog(@"Upload Failed"); 
       return; 
      } 
      NSLog(@"error: %@", [operation error]); 

     }]; 

     [httpClient enqueueHTTPRequestOperation:operation]; 
    }} 

希望這會幫助別人!

2

試試這個,讓我們知道,如果它的工作原理:

 NSData *imageToUpload = UIImageJPEGRepresentation(uploadedImgView.image,1.0);//(uploadedImgView.image); 
     if (imageToUpload) 
     { 
     NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 
     [parameters setObject:@"MY API KEY" forKey:@"key"]; 
     [parameters setObject:@"json" forKey:@"format"]; 

     AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"https://post.imageshack.us"]]; 

     NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 
      [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"temp.jpeg" mimeType:@"image/jpeg"]; 
     }]; 

     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

     [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
     { 
      NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil]; 
      //NSLog(@"response: %@",jsons); 

     } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) 
     { 
      if([operation.response statusCode] == 403) 
      { 
       //NSLog(@"Upload Failed"); 
       return; 
      } 
      //NSLog(@"error: %@", [operation error]); 

     }]; 

     [operation start]; 
    } 
+0

仍然相同的JSON響應錯誤 – Oleg

+0

這裏是一個問題的鏈接可以幫助你:http://stackoverflow.com/questions/ 931088/http-post-to-imageshack –

1

上傳影像(基本)

Endpoint : https://post.imageshack.us/upload_api.php Parameters : 
* key : The api key for your application; found in email sent after filling out our API Key Request form 
* fileupload : image file 
* format : json tags : a CSV list of tags to describe your picture public : A string setting your image to public or private where "yes" is public and "no" is private. Images are public by default. 

你使用密鑰請求,並得到你自己的上傳過程中的關鍵?

Obtaining an API Key 
To obtain an API key, please use our API Key Request form. 

還設置格式application/json

+0

是的,我使用密鑰。它告訴我一個錯誤,我應該這樣做,因爲我沒有使用它,所以我確定問題與API密鑰 – Oleg

+0

無關,您設置了答案和測試中指定的格式? –

+0

是的,我做到了,沒有運氣:-( – Oleg