I am having the below code in view for the file upload form:
<form method="post" action="<?=site_url('api/do_upload')?>" enctype="multipart/form-data" id="upload_photo" />
<input type="file" name="userfile" size="20" class="btn btn-primary" /><br />
<input type="submit" value="upload" class="btn btn-warning btn-xs" />
</form>
The event captures in JS:
$("#upload_photo").submit(function(evt) {
evt.preventDefault();
var url = $(this).attr('action');
var postData = $(this).serialize();
$.post(url, postData, function(o){
if(o.result == 1) {
Display.success(o.output);
}
else
{
Display.error(o.error);
}
},'json');
});
Model, where I am processing the file upload:
public function do_upload()
{
$this->_require_login();
$this->output->set_content_type('application_json');
$config['upload_path'] = 'C:\xampp2\htdocs\kedb\public\img\profile';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['file_name'] = $this->session->userdata('user_id');
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$this->output->set_output(json_encode([
'result' => '0',
'output' => $this->upload->display_errors()
]));
}
else
{
$this->output->set_output(json_encode([
'result' => '1',
'output' => 'File Uploaded Successfully'
]));
return false;
//$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
}
When I click the "upload" button, it getting the below error:
{"result":"0","output":"<p>You did not select a file to upload.<\/p>"}
If I remove id="upload_photo" in <form> tag, it is working. It gives error only when I add id attribute in <form>.
I might have missed something or did anything wrong. Could someone please me out?