このような質問がいくつかありますが、すべてチェックしましたが、役に立たないようです。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>
EDIT:ヘッダーにmalsup.com jQuery Form Pluginを含めています