我試圖用自己的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;
}
任何人都可以幫助我嗎?什麼是正確的方法來做到這一點?
你試過發送'NSData'而不是'NSString'嗎? –
是的,我做了,並得到相同的錯誤響應 – Oleg
你有沒有嘗試過一個非常小的圖像? /確保它不是超時問題 – 2013-06-05 08:40:16