如何上传图像阵列以及其他参数,图像是使用设备相机拍摄的,需要上传。 到目前为止,我已经尝试过以下代码:
将捕获的图像转换为NSData:
for(int i = 0 ; i< imageArray.count ; i++)
{
UIImage *images = [imageArray objectAtIndex :i];
NSData *Datas = UIImagePNGRepresentation(images);
[Images addObject:Datas];
}
检索已保存的图像数组:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
arrayOfImages = [userDefaults objectForKey:@"image"];
将图像上传到服务器:
NSDictionary *params =@{ @"name":self->Name.text, @"contact_no":self->ContactNo.text,@"email_id":self->EmailId.text,@"s_date":Date,@"s_time":Time,@"streat":Street,@"city":City,@"state":State,@"zip":ZipCode};
NSData *uploadData = Data;
NSString *urlString = [NSString stringWithFormat:@"url"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSString *kNewLine = @"\r\n";
NSMutableData *body = [NSMutableData data];
for (NSString *name in params.allKeys) {
NSData *values = [[NSString stringWithFormat:@"%@", params[name]] dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:[[NSString stringWithFormat:@"--%@%@", boundary, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", name] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@%@", kNewLine, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:values];
[body appendData:[kNewLine dataUsingEncoding:NSUTF8StringEncoding]];
}
for(int i = 0; i < arrayOfImages.count ; i++)
{
uploadData = [arrayOfImages objectAtIndex:i];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file_name[]\""] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:uploadData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
[request setHTTPBody:body];
[request setHTTPBody:body];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",returnString);
}];
PHP代码:
$filesCount = count($_FILES['file_name']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['file_name1']['name'] = time().'_'.$_FILES['file_name']['name'][$i];
$_FILES['file_name1']['type'] = $_FILES['file_name']['type'][$I];
$_FILES['file_name1']['tmp_name'] = $_FILES['file_name']['tmp_name'][$i];
$_FILES['file_name1']['error'] = $_FILES['file_name']['error'][$i];
$_FILES['file_name1']['size'] = $_FILES['file_name']['size'][$i];
$config['upload_path'] = $upload_path;
$config['allowed_types'] = '*';
$this->load->library('upload');
$this->upload->initialize($config);
if($this->upload->do_upload('file_name1')){
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name'];
$fileName = $upload_data['file_name'];
$images[] = $fileName;
}else{
$data['upload_errors'][$i] = $this->upload->display_errors();
}
}
答案 0 :(得分:0)
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@“Your url”]];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:200];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", nil];
AFHTTPRequestOperation *operation = [manager POST:@"url"parameters:_paramaters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileData:imagedata name:@"filename" fileName:@"filename.jpg" mimeType:@"image/jpeg"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
}];
[operation start];
您可以使用AFHTTPRequestOperationManager,代码在上面,它对我有用。