javascript - jquery and ajax file uploader won't work -
javascript - jquery and ajax file uploader won't work -
i seek follow sample code of jquery , ajax file uploader i've got error. cannot figure out error because when press submit button, nil happens. please help me error.
the controller:
<?php class upload extends ci_controller { public function __construct() { parent::__construct(); $this->load->model('files_model'); $this->load->database(); $this->load->helper('url'); } public function index() { $this->load->view('upload'); } public function upload_file() { $status = ""; $msg = ""; $file_element_name = 'userfile'; if (empty($_post['title'])) { $status = "error"; $msg = "please come in title"; } if ($status != "error") { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|doc|txt'; $config['max_size'] = 1024 * 8; $config['encrypt_name'] = true; $this->load->library('upload', $config); if (!$this->upload->do_upload($file_element_name)) { $status = 'error'; $msg = $this->upload->display_errors('', ''); } else { $data = $this->upload->data(); $file_id = $this->files_model->insert_file($data['file_name'], $_post['title']); if($file_id) { $status = "success"; $msg = "file uploaded"; } else { unlink($data['full_path']); $status = "error"; $msg = "something went wrong when saving file, please seek again."; } } @unlink($_files[$file_element_name]); } echo json_encode(array('status' => $status, 'msg' => $msg)); } public function files() { $files = $this->files_model->get_files(); $this->load->view('files', array('files' => $files)); } public function delete_file($file_id) { if ($this->files_model->delete_file($file_id)) { $status = 'success'; $msg = 'file deleted'; } else { $status = 'error'; $msg = 'something went wrong when deleteing file, please seek again'; } echo json_encode(array('status' => $status, 'msg' => $msg)); } } ?>
the model:
<?php class files_model extends ci_model { public function insert_file($filename, $title) { $data = array( 'file_name' => $filename, 'file_path' => $title ); $this->db->insert('files', $data); homecoming $this->db->insert_id(); } public function get_files() { homecoming $this->db->select() ->from('files') ->get() ->result(); } public function delete_file($file_id) { $file = $this->get_file($file_id); if (!$this->db->where('id', $file_id)->delete('files')) { homecoming false; } unlink('./files/' . $file->filename); homecoming true; } public function get_file($file_id) { homecoming $this->db->select() ->from('files') ->where('id', $file_id) ->get() ->row(); } } ?>
the view:
<!doctype html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script src="<?php echo base_url()?>js/site.js"></script> <script src="<?php echo base_url()?>js/ajaxfileupload.js"></script> <link href="<?php echo base_url()?>css/style.css" rel="stylesheet" /> </head> <body> <h1>upload file</h1> <form method="post" action="" id="upload_file"> <label for="title">title</label> <input type="text" name="title" id="title" value="" /> <label for="userfile">file</label> <input type="file" name="userfile" id="userfile" size="20" /> <input type="submit" name="submit" id="submit" /> </form> <h2>files</h2> <div id="files"></div> </body> </html>
the javascript:
$(function() { $('#upload_file').submit(function(e) { e.preventdefault(); $.ajaxfileupload({ url :'./upload/upload_file/', secureuri :false, fileelementid :'userfile', datatype : 'json', info : { 'title' : $('#title').val() }, success : function (data, status) { if(data.status != 'error') { $('#files').html('<p>reloading files...</p>'); refresh_files(); $('#title').val(''); } alert(data.msg); } }); homecoming false; }); }); function refresh_files() { $.get('./upload/files/') .success(function (data){ $('#files').html(data); }); } $('.delete_file_link').live('click', function(e) { e.preventdefault(); if (confirm('are sure want delete file?')) { var link = $(this); $.ajax({ url : './upload/delete_file/' + link.data('file_id'), datatype : 'json', success : function (data) { //files = $(#files); files = $(files); if (data.status === "success") { link.parents('li').fadeout('fast', function() { $(this).remove(); if (files.find('li').length == 0) { files.html('<p>no files uploaded</p>'); } }); } else { alert(data.msg); } } }); } });
javascript jquery ajax
Comments
Post a Comment