我正在尝试通过ajax _call将文件表单视图发送到服务器。 这是我的表格:
<form id="fileForm" name="fileForm" enctype="multipart/form-data">
<div class="row" >
<div class="span8" >
<label><strong>Please attach a file</strong></label>
<input style="margin-bottom:0" type="file" name="attach" id="attach" />
<button type="submit" class="btn btn-primary" onclick="do_upload()" ><i class="icon-camera icon-white"></i>Send</button>
</div>
</div>
</form>
这是do_upload(在同一个文件中):
<script type="text/javascript">
function do_upload()
{
var uploaded_file= $('#attach').val();
alert(uploaded_file); //prints the name of the uploaded file
$.ajax({
type:'POST',
url : '<?php echo site_url()."/public/hours/do_upload/"; ?>', //the path to my controller
data : uploaded_file,
cache: false,
contentType: 'multipart/form-data',
processData :false ,
success:function(data){
alert(' sucessful ajax call ');//is printed
}
});
}
</script>
最后在公共/小时(这是我的控制者),我有:
function do_upload () {
$config= array ('upload_path'=>'./uploads/','allowed_types'=>'pdf|gif|jpg|jpeg|docx', 'max_size'=>2048);
//loading upload
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('attach'))
{ $data = array('error' => $this->upload->display_errors());
var_dump($data);
exit;
}
else
{ $data = array('upload_data' => $this->upload->data('attach'));
var_dump($data);
exit;
}
}
这是var_dump($ data)的结果:
array(1) {
["error"]=>
string(43) "<p>You did not select a file to upload.</p>"
}
萤火虫显示了这个错误:
Form contains a file input, but is missing method=POST and enctype=multipart/form-data on the form. The file will not be sent.
有人可以帮我吗?谢谢
答案 0 :(得分:1)
尝试输入type =“button”或使用
上的<div class="btn btn-primary" onclick="do_upload()">..</div>
<button type="submit" class="btn btn-primary" onclick="do_upload()" ><i class="icon-camera icon-white"></i>Send</button>
它对我有用..