我正在使用AFNetworking将图像和几个参数一起上传到PHP脚本(使用CodeIgniter构建),该脚本将接收图像,将文件名和参数放入数据库并移动图像到永久的位置。AFNetworking上传图像间歇性地工作
这里的对象 - 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('[email protected]', '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似乎允许不同大小的图像很好...但大或小,它可能只会在第二次尝试。
的思考?
您很可能犯了一个错误 - 但是,如果没有看到代码,任何人都能够帮助您? – vikingosegundo
相关问题:http://stackoverflow.com/questions/4359531/nsmutableurlrequest-and-request-body-stream-exhausted-error – Felix
这是发生在模拟器还是只在设备上?设备上是否有足够的磁盘空间? setUploadProgressBlock中的代码是否正在执行? – Felix