我正在使用 AFNetworking 将图像和一些参数上传到 PHP 脚本(使用 CodeIgniter 构建),该脚本将接收图像,将文件名和参数放入数据库并将图像移动到永久位置。
这是 Obj-C:
NSURL *url = [NSURL URLWithString:@"http://my_api_endpoint"];
NSData *imageToUpload = UIImageJPEGRepresentation(_mainMedia, .25f);
NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:
_topicText.text,@"Topic",
@"1",@"Category",
@"1",@"Creator",
nil];
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:url];
NSString *timeStamp = [NSString stringWithFormat:@"%0.0f.jpg", [[NSDate date] timeIntervalSince1970]];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"apidebate/debates" parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData: imageToUpload name:@"MainMedia" fileName:timeStamp mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *response = [operation responseString];
NSLog(@"response: [%@]",response);
[self dismissViewControllerAnimated:YES completion:nil];
[self.delegate createTopicViewControllerDidCreate:self];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if([operation.response statusCode] == 403){
NSLog(@"Upload Failed");
return;
}
NSLog(@"error: %@", [operation error]);
[self dismissViewControllerAnimated:YES completion:nil];
[self.delegate createTopicViewControllerDidCreate:self];
}];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
float width = totalBytesWritten / totalBytesExpectedToWrite;
}];
[operation start];
这是PHP:
//CONTROLLER FROM API
function debates_post()
{
mail('myemailaddress@gmail.com', 'Test', 'Posted');
$tmp_dir = "images/posted/";
if(isset($_FILES['MainMedia'])){
$SaniFileName = preg_replace('/[^a-zA-Z0-9-_\.]/','', basename($_FILES['MainMedia']['name']));
$file = $tmp_dir . $SaniFileName;
move_uploaded_file($_FILES['MainMedia']['tmp_name'], $file);
}
else
$SaniFileName = NULL;
$data = array('Topic'=>$this->post('Topic'), 'MainMedia'=>$this->post('MainMedia'), 'Category'=>$this->post('Category'), 'Creator'=>$this->post('Creator'));
$insert = $this->debate->post_debate($this->post('Topic'), $SaniFileName, $this->post('Category'), $this->post('Creator'));
if($insert){
$message = $this->db->insert_id();
}
else{
$message = 'Insert failed';
}
$this->response($message, 200);
}
//MODEL
function post_debate($Topic=NULL, $MainMedia='', $Category=NULL, $Creator=NULL){
$MainMedia = ($MainMedia)?$MainMedia:'';
$data = array(
'Topic' => $Topic,
'MainMedia' => $MainMedia,
'Category' => $Category,
'Creator' => $Creator,
'Created' => date('Y-m-d h:i:s')
);
return $this->db->insert('debate_table', $data);
}
我目前的问题是从 iOS 上传很少完成,并且没有模式可以完成。我可以只用参数添加大照片或小照片,或者根本不添加照片,它的工作时间为 20%。当它失败时,这是我在 X-Code 中收到的错误消息:
2012-08-26 01:52:10.698 DebateIt[24215:907] error: Error Domain=NSURLErrorDomain
Code=-1021 "request body stream exhausted" UserInfo=0x1dd7a0d0
{NSErrorFailingURLStringKey=http://my_api_url,
NSErrorFailingURLKey=http://my_api_url,
NSLocalizedDescription=request body stream exhausted,
NSUnderlyingError=0x1e8535a0 "request body stream exhausted"}
这到底是什么东西?
我有一个基本的 HTML 表单,我将相同的图像发布到同一个端点,并且每次都适用于任何大小的图像。iOS似乎允许不同大小的图像很好......但无论大小,它可能只会在第二次尝试时起作用。
想法?