我在codeigniter中收到错误'你没有选择要上传的文件'。不要我做错了,这是我的代码。
查看
<form action="#" method="post" id="svf-form-4" enctype="multipart/form-data">
<div class="row clearfix">
<div class="col-md-6 bottommargin-sm photograph_part">
<label>Upload Your Photograph</label><br>
<input id="photograph" name="photograph" type="file" class="file">
</div>
</div>
<button class='button button-rounded nomargin' id='scfbutton4' value='next'>Submit</button>
</form>
的Ajax:
$('#scfbutton4').click(function(e){
$.post("<?php echo BASE_URL.'/startcf/save_form4';?>", $('#svf-form-4').serialize(),function(data){
console.log(data); //Output: {a: {error: "<p>You did not select a file to upload.</p>"}}
}
e.preventDefault();
});
控制器
public function save_form4(){
$config['upload_path'] = './uploads/photograph';
$config['allowed_types'] = 'jpg|png|pdf';
$config['max_size'] = 1024;
$this->load->library('upload', $config);
if($this->upload->do_upload('photograph'))
{
//$data = array('upload_data' => $this->upload->data());
header('Content-Type: application/json');
echo json_encode( array('a' => 1) );
return;
}
else{
$error = array('error' => $this->upload->display_errors());
header('Content-Type: application/json');
echo json_encode( array('a' => $error) );
return;
}
}
有人可以帮我找到我做错的事吗?
答案 0 :(得分:2)
尝试此代码:
上传文件时序列化无效,请尝试以下方法:
<script type="text/javascript">
function go_form(){
$.ajax({
type:'POST',
url:'testing2.php',
data:new FormData($('#svf-form-4')[0]),
cache: false,
contentType: false,
processData: false,
success:function(msg){
$('#message').html(msg);
}
});
return false;
}
</script>
在表单上提交此调用方法。