有几个这样的问题,但我已经检查了所有问题,但它们似乎对我没有帮助。我正在使用 CodeIgniter 2.1.4,我正在尝试使用 ajax 和文件上传类上传文件。无论我尝试什么,我总是收到错误“您没有选择要上传的文件”。
我有控制器:
class Img extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('img/index');
}
public function upload()
{
$imgfile = $_POST['imgfile'];
$config['upload_path'] = 'upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('imgfile'))
{
$data = array('error' => $this->upload->display_errors('', ''));
$result['success'] = 1;
$result['message'] = $data['error'];
}
else
{
$data = array('upload_data' => $this->upload->data());
$result['success'] = 0;
$result['message'] = "Image Uploaded";
}
die(json_encode($result));
}
并将视图“img/index”视为:
<div id="alert" class="alert"> </div>
<form id="addForm" action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="imgfile" id="imgfile" size="20" />
<button type="submit" id="submit_add" name="submit_add">Submit</button>
</form>
<script type="text/javascript">
$("#addForm").submit(function(e)
{
e.preventDefault();
var imgfile = $("#imgfile").val();
$.ajax({
type: "POST",
url: "?c=img&m=upload",
dataType: "json",
data: 'imgfile='+imgfile,
cache: false,
success: function(result){
$('#alert').empty().append(result.message);
}
});
});
</script>
编辑:我在标题中包含了 malsup.com jQuery 表单插件