2013-05-30 44 views
6

Tôi đang cố gắng để tải lên một hình ảnh lên ImageShack sử dụng API của họ: thông báo lỗiiOS - hình ảnh tải lên với 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]; 
    } 
} 

Như một phản ứng tôi nhận được lại không có lời giải thích lỗi:

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

Có ai giúp tôi với điều đó không? Cách thích hợp để làm điều đó là gì?

+0

bạn đã thử gửi 'NSData' thay vì' NSString' chưa? –

+0

có, tôi đã làm và nhận được phản hồi lỗi tương tự – Oleg

+0

bạn đã thử với một hình ảnh rất nhỏ?/để đảm bảo nó không phải là một vấn đề thời gian chờ –

Trả lời

3

Được rồi, tôi đã quản lý để giải quyết vấn đề của tôi. Tất cả các tham số phải được đặt trong phần thân của biểu mẫu, không phải là giá trị yêu cầu. Có vẻ khá đơn giản:

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]; 
    }} 

Hy vọng điều này sẽ giúp ai đó!

2

Hãy thử điều này và cho chúng tôi biết nếu nó hoạt động:

 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

Vẫn cùng một phản ứng JSON với lỗi – Oleg

+0

đây là một liên kết của một câu hỏi có thể giúp bạn: http://stackoverflow.com/questions/ 931088/http-post-to-imageshack –

1

tải lên một hình ảnh (cơ bản)

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. 

Bạn có sử dụng yêu cầu quan trọng và nhận được chìa khóa của riêng bạn cho quá trình tải lên?

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

Ngoài ra thiết lập định dạng application/json

+0

Có, tôi sử dụng phím. Nó cho tôi thấy một lỗi mà tôi nên làm điều đó khi tôi không sử dụng nó, vì vậy tôi chắc chắn vấn đề không liên quan đến khóa API – Oleg

+0

bạn đặt định dạng được chỉ định trong câu trả lời và kiểm tra? –

+0

Vâng, tôi đã làm, không may mắn :-( – Oleg

Các vấn đề liên quan