6

我正在尝试使用他们的API将图片上传到 ImageShack :

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

任何人都可以帮助我吗?正确的方法是什么?

4

3 回答 3

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

希望这会对某人有所帮助!

于 2013-06-07T12:14:08.183 回答
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];
    }
于 2013-05-30T09:34:28.253 回答
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

于 2013-05-30T09:36:04.073 回答