html代码:
<form action="" method="POST" role="form" id="registration_form" enctype="multipart/form-data">
<label> File Input: </label>
<input type="file" name="userfile" id="userfile">
<input type="submit" name="submit" value="Upload" />
</form>
查看页面上的Js: $:
<script src="../../js/jquery-validation.js"></script>
<script>
$(document).ready(function() {
$("#registration_form").validate({ submitHandler: submitForm });
function submitForm() {
var photo = $('#userfile').val();
alert(photo);
$.ajax({
url: "<?php echo base_url();?>upload/do_upload",
type: "POST",
data: { user_photo: photo },
success: function (data) { alert(data); }
});
}
});
</script>
控制器代码:
<?php
class Upload extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('registration_model');
}
public function index(){
$this->load->view('upload_view');
}
public function do_upload() {
$config = array( 'upload_path' => "/var/www/html/upload/", 'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|docx");
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userfile')){
$error = array('error' => $this->upload->display_errors());
print_r( $error);
} else{
$data = array('upload_data' => $this->upload->data());print_r($data);
}
}
}
?>