我在CodeIgniter 2.1.4上遇到麻烦,并通过AJAX上传文件。
我目前有一个完美提交的表单,如此( views / load_form ):
<!--form created to add member to database -->
<?php echo form_open_multipart('index.php/member_record/add_member'); ?>
<?php echo form_fieldset('Create Membership Record'); ?>
<div class="container">
<form id="memberform" role="form">
<?php echo form_label('First Name', 'fname'); ?>
<?php echo form_input('fname'); ?>
</div>
<!--other such fields omitted for brevity-->
<label>Upload scanned ID Card</label>
<input type="file" name="scIDFile" id="scIDFile" />
<p class="help-block">Select image as either JPEG, GIF, PNG</p>
<input id="submit" type="submit" name="submit" value="Create Record" class="btn btn-success"/>
<!--end of view-->
我有一个控制器可以很好地处理这个( controllers / member_record ):
public member_record extends CI_Controller{
public add_member(){
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['max_size']= 1024*8;
$config['encrypt_name']=TRUE;
$config['overwrite'] =TRUE;
$this->load->library('upload',$config);
foreach($_FILES as $field => $file){
if($file['error'] == 0){
if($this->upload->do_upload($field)){
/*$image_config=array(
'source_image'=>$data['full_path'],
'new_image'=>$data['file_path'].'/thumbs',
'maintain_ratio'=>true,
'width'=>150,
'height'=>100
);
$this->load->library('image_lib',$image_config);
$this->image_lib->resize();*/
}else{
$errors = $this->upload->display_errors();
}
}
}
}
}
这很好。但是,由于我添加到我的视图中的某些新功能:我需要将事物转换为AJAX并通过AJAX将所有这些值传递给后端。我已经能够通过POST将所有字段(如名字,姓氏)提交到后端,但是我努力启用文件上传! 我尝试过使用jQuery文件上传插件,但是关于如何将它们用于CodeIgniter 2(特别是考虑到我当前的代码)还没有明确的指示。如果有人能对此有所了解,我将不胜感激。