文件没有上传,当我使用jquery ajax方法调用CodeIgniter的控制器函数时,给我"您没有选择要上传的文件"错误。所以请帮我解决这个错误,并上传文件与ajax
//My View :
<form id="frm" name="frm" enctype="multipart/form-data">
<input type="file" name="userfile" id="userfile" size="20"/>
<input type="button" value="upload" id="upload" name="upload"/>
</form>
//calling button live method from view
$('#upload').live("click", function ()
{
//calling jquery ajax
$.ajax({
type: "POST",
url:"../user/usercontroller/do_upload", //calling controller function
cache: true,
success: function(data4)
{
alert('in ajax'+data4);
},datatype: "json"
});
});
//My Controller:
function do_upload()
{
$upload_dir ='./uploads/';
$config['upload_path'] = realpath($upload_dir);
$config['overwrite'] = TRUE;
$config['allowed_types'] = '*';
$config['encrypt_name'] = true;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile'))
{
print_r($this->upload->display_errors());
}
else
{
print_r($this->upload->data());
}
}
不能使用Ajax上传文件。如果你想让它看起来像ajax,那么将窗体的目标属性设置为隐藏iFrame。
您需要在$.ajax()
呼叫中设置数据。你现在提交的是一篇空白文章。
试着这样写:
$.post("../user/usercontroller/do_upload", //calling controller function
$('#frm').serialize(),
function(data4){
alert('in ajax'+data4);
},
"json"
);