javascript - Upload File Using Ajax in Codeigniter -
i trying upload file other data. thing i'm getting error you did not select file upload. can't understand i'm doing wrong. glad if can point out doing wrong.
html file
<div class="modal-body"> <form id="add_message" enctype="multipart/form-data" method="post" action="<?php echo base_url() ?>apps/messages/composemessage"> <div class="form-group"> <select id="messageto" class="form-control" data-plugin="select2" multiple="multiple" data-placeholder="to:" name="messageto"> </select> </div> <div class="form-group"> <input class="form-control" placeholder="subject" id="messagesubject" name="messagesubject"></input> </div> <div class="form-group"> <textarea data-provide="markdown" data-iconlibrary="fa" rows="10" id="messagebody" name="messagebody"></textarea> </div> <input type="hidden" name="fname" value="" id="formname" ></input> <div class="left"> <!-- <span class="btn green fileinput-button"> --> <input id="message_attachement" type="file" name="file_attac" size="20" > <!-- </span> --> </div> <!-- <button class="btn btn-primary" type="submit" value="submit">send</button> --> </form> </div> <div class="modal-footer text-left"> <button class="btn btn-primary" data-dismiss="modal" id="addformbutton">send</button> <button class="btn btn-primary" data-dismiss="modal" id="saveformbutton">save</button> <a class="btn btn-sm btn-white btn-pure" data-dismiss="modal" href="javascript:void(0)">cancel</a> </div>
js
$("#addformbutton").on('click' , function(){ debugger; $.ajax({ type: "post", url: base_url + "apps/messages/composemessage", async: false, mimetype: "multipart/form-data", datatype:json, data:{ 'reciever': $('#messageto').val() , 'subject': $('#messagesubject').val(), 'text': $('#messagebody').val(), 'type': 'active', 'msgid': $('#formname').val(), 'attachment' : $('#message_attachement').val() }, success: function(response){ }, error: function(response){ } }); });
controller
$this->load->helper('file_upload'); $filename = $this->input->post('attachment'); $path = 'uploads/attachments/'; $allowed_types = 'erb|php|txt'; $redirect = ''; // error_log("outside"); if (!empty($this->input->post('attachment'))) { // error_log("inside"); // error_log ("parameters: " . $path." types: ". $allowed_types." name: ". $filename." redirect: ". $redirect); $parameters['profile_pic'] = file_upload($path, $allowed_types, $filename, $redirect); // error_log("the path "); // error_log($parameters['profile_pic']); if ($this->session->set_userdata("img_errors")) { // error_log("error"); return false; } }
file upload function
function file_upload($upload_path , $allowed_types , $filename , $redirect) { $ci = & get_instance(); $config['upload_path'] = $upload_path; $config['allowed_types'] = $allowed_types; // $config['max_size'] = 1024;//1mb // $config['max_width'] = 1024; // $config['max_height'] = 1024; $ci->load->library('upload', $config); $data = null; if (!$ci->upload->do_upload($filename)) { error_log("within file"); // $error = array('error' => $ci->upload->display_errors()); error_log($ci->upload->display_errors()); $ci->session->set_userdata('img_errors', $ci->upload->display_errors()); //error_log(print_r($ci->upload->display_errors(),true)); // redirect(base_url() . $redirect); } else { error_log("uploading"); $data = array('upload_data' => $ci->upload->data()); // do_resize($config['upload_path'] , $data['upload_data']['file_name']); } return $config['upload_path'] . $data['upload_data']['file_name']; }
this working code used in recent project, code self explanatory feel free ask question.
html:
<form action="http://localhost/index.php/upload_file" method="post" style="display:none;" id="file_upload_form" enctype="multipart/form-data"> <input type="file" id="dialog_triggerer" name="uploaded_file"> </form> <button class="btn btn-default btn-fab-sm" id="file_attach"> <span class="mdi-file-attachment"></span> </button>
js:
trigger code on action:
var form = $('form')[0]; // standard javascript object here var formdata = new formdata(form); if($("#dialog_triggerer").val()!=""){ $.ajax( { url: frontend_url + '/upload_file', type: 'post', data: formdata, processdata: false, contenttype: false, async: false } ).done(function(data){ file_data = json.parse(data); new_post.file_data = file_data; }); }
upload_file ctrl:
<?php class upload_file extends ci_controller{ public function __construct(){ parent::__construct(); } public function index(){ $valid_file=true; $message; //if did upload file... if($_files['uploaded_file']['name']) { //if no errors... if(!$_files['uploaded_file']['error']) { //now time modify future file name , validate file $new_file_name = strtolower($_files['uploaded_file']['name']); //rename file if($_files['uploaded_file']['size'] > (20024000)) //can't larger 20 mb { $valid_file = false; $message = 'oops! file\'s size large.'; } //if file has passed test if($valid_file) { $file_path = 'themes/uploads/'.$new_file_name; move_uploaded_file($_files['uploaded_file']['tmp_name'], fcpath . $file_path); $message = 'congratulations! file accepted.'; } } //if there error... else { //set returned message $message = 'ooops! upload triggered following error: '.$_files['uploaded_file']['error']; } } $save_path = base_url().$file_path; $name = $_files['uploaded_file']['name']; $size = $_files['uploaded_file']['size']; $type = $_files['uploaded_file']['type']; $data = array( "message" => $message, "save" => $save_path, "name" => $name, "size" => $size, "type" => $type ); $this->load->view('upload_file/upload_file.php', $data); } }
upload_file.php view:
<?php $res = array( "msg" => $message, "path" => $save, "name" => $name, "size" => $size, "type" => $type ); echo json_encode($res); ?>
Comments
Post a Comment