【发布时间】:2012-09-26 16:00:04
【问题描述】:
我正在尝试通过 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.
谁能帮帮我?谢谢
【问题讨论】:
-
你的错误表明你在表单声明中缺少
method=POST,你试过添加这个吗? -
@mr_lewjam 是的,我添加了这个,但我得到了同样的错误信息