我需要使用表单数据提交图像,我已经为此搜索了一个更好的方法,但遗憾的是我找不到更好的方法来实现我的目的,以前使用this.serialize()方法完成。但它不适用于图像
这是我的代码
查看
<?php
$this->load->helper('form');
$attributes = array('method'=>'post','name'=>'create_company','id'=>'create_company');
echo form_open_multipart('',$attributes);?>
<label>Code : </label> <?php echo form_input('code');?><br/><br/>
<label>Name : </label> <?php echo form_input('name');?><br/><br/>
<label>Logo : </label><input type="file" name="userfile"/><br/><br/>
<label>URL : </label> <?php echo form_input('url');?><br/><br/>
<label>Description : </label> <textarea name="description" rows="4" cols="50"> </textarea><br/><br/>
<input type="submit" name="submit" value="Save"/>
<?php echo form_close(); ?>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>//no need to specify the language
$(document).ready(function() {
$('#create_company').on("submit",function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "<?php echo site_url('site/upload'); ?>",
data: $(this).serialize(),
success: function(data){
var site_url = "<?php echo site_url('site/create_branch_form'); ?>";
var json = $.parseJSON(data);
site_url = site_url +"/" + json.results[0].id ;
alert(site_url);
$("#content").load(site_url);
alert(data);
}
});
});
});
</script>
控制器
public function upload(){
//insert company details
$this->load->model('company_model');
$data['results'] = $this->company_model->insert_company();
$this->output->set_output(json_encode($data));
}
模型
function insert_company(){
//user details
$username = $this->session->userdata('username');
//$query = $this->db->get_where('userdetails', array('username' => $username));
$query = $this->db->get_where('userdetails',array('username'=>$username));
foreach ($query->result() as $function_info)
{
$this->userid = $function_info->id;
}
$new_company_insert_data = array(
'code' => $this->input->post('code'),
'name' => $this->input->post('name'),
'logo' => $imgpath['file_name'],
'url' => $this->input->post('url'),
'description' => $this->input->post('description'),
'createdat' => date('Y-m-d H:i:s',strtotime('')),
'status' => 0,
'userid' => $this->userid
);
$this->db->insert('companydetails',$new_company_insert_data);
//get id of the last inserted row
$this->load->database();
$query = $this->db->query("SELECT MAX(id) AS id FROM companydetails");
return $query->result();
}
答案 0 :(得分:2)
尝试使用FormData
对象。你应该在JS代码中有这样的东西:
$(document).ready(function() {
$('#create_company').on("submit",function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
type: "POST",
url: "<?php echo site_url('site/upload'); ?>",
data: formData,
cache: false,
contentType: false,
processData: false
success: function(data){
//function success
},
error: function(data){
//error function
}
});
});
});
希望这可以提供帮助。
答案 1 :(得分:1)
答案 2 :(得分:0)
使用jquery.form.js上传包含附件的表单数据。 并在控制器中添加此功能。
function uploadFile($file, $uploads_dir, $allowfiles) {
if ($file['size'] > 0) {
$pic_name = "";
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
if (in_array($ext, $allowfiles)) {
$pic_name = rand();
$tmp_name = $file["tmp_name"];
$pic_name.= $file["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$pic_name");
}
return $pic_name;
} else {
return "";
}
}