主要问题是所有文件都上传除" .docx"文件。
我正在使用 var formData = new FormData(); ,然后我将文件附加到其中。
甚至奇怪的是我无法在控制器中获取文件。 ajax调用甚至没有进入控制器。 如果我上传PDF或任何其他文件,那么它应该正常工作。
我既没有在firebug的控制台中出现任何错误,也没有在firebug选项卡的网络选项卡中显示任何响应。 我在ajax调用上设置了ajax加载器,它只是继续加载。 什么都没发生,然后我将不得不刷新页面。
ajax调用甚至无法达到控制器功能。
以下是我的代码。
是的,感谢所有stackoverflow开发人员提供的所有早期支持。
我上传了两个文件,两个文件都可以采用以下函数允许的格式。
这是查看代码
$("#cover-letter").change(function(){
var file = this.files[0];
name = file.name;
size = file.size;
type = file.type;
var get_ext = name.split('.');
get_ext = get_ext.reverse();
if (get_ext[0].toLowerCase() == 'wps' || get_ext[0].toLowerCase() == 'doc' || get_ext[0].toLowerCase() == 'docx' || get_ext[0].toLowerCase() == 'pdf'){
$('#img_error').hide();
readURL(this);
} else {
$('#img_error').show();
$('#img_error').css('color','red');
$('#img_error').text("Invalide file format");
$("#cvr_name").text('Attach Cover Letter');
$(this).val('');
$('#img_name').empty();
}
});
$("#resume").change(function(){
var file = this.files[0];
name = file.name;
size = file.size;
type = file.type;
var get_exts = name.split('.');
get_exts = get_exts.reverse();
if ( get_exts[0].toLowerCase() == 'wps' || get_exts[0].toLowerCase() == 'doc' || get_exts[0].toLowerCase() == 'docx' || get_exts[0].toLowerCase() == 'pdf' ){
$('#resume_error').hide();
readDocsURL(this);
} else {
$('#resume_error').show();
$('#resume_error').css('color','red');
$('#resume_error').text("Invalide file format");
$("#res_val").text('Attach Resume');
$(this).val('');
// $('#resume_error').attr("disabled", true);
$('#res_name').empty();
}
});
// image name
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
// $('#blah').attr('src', e.target.result);
var remove = '<img id="cross-remove" class="cross-remove" src="'+IMAGE_URL+'cross-remove.png">';
var imgpath = $("#cover-letter").val();
if(imgpath != '') {
document.getElementById("img_name").textContent = imgpath;
$("#img_name").append(remove);
} else {
document.getElementById("img_name").textContent = '';
}
}
reader.readAsDataURL(input.files[0]);
}
}
// resume or docs url
function readDocsURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
// $('#blah').attr('src', e.target.result);
var remove = '<img id="cross-remove" class="cross-remove" src="'+IMAGE_URL+'cross-remove.png">';
var respath = $("#resume").val();
if(respath != '') {
document.getElementById("res_name").textContent = respath;
$("#res_name").append(remove);
} else {
document.getElementById("res_name").textContent = '';
}
}
reader.readAsDataURL(input.files[0]);
}
}
$('.addimages').click(function(e) {
$("#validateForm").validate({
submitHandler: function(form) {
$("#loading").css("display","block");
var formData = new FormData();
formData.append( 'coverletter', $( '#cover-letter' )[0].files[0] );
formData.append( 'resume', $('#resume')[0].files[0] );
formData.append( 'comment', $( '#comment' ).val() );
formData.append( 'iAdvertiseID', $( '#iAdvertiseID' ).val() );
console.log($( '#cover-letter' )[0].files[0]);
$.ajax({
url: "<?php echo base_url('application_form/do_upload'); ?>",
type: "POST",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(data){
"<?php unset($_POST); ?>";
$('#res_name').empty();
$('#img_name').empty();
$("#validateForm").get(0).reset();
var msg = "<div id='alert'><div class='messege widthinherit martop10 messege-info'>"+data+"</div></div>";
$('html, body').animate({scrollTop:0}, 'slow');
$("#loading").css("display","none");
$("#appendme").html(msg).fadeOut(3000);
}
});
}
});
});
控制器代码
$iApplicationID = $this->application_form_model->addApplication();
$emailInfo = $this->application_form_model->getEmailInfo($_POST['iAdvertiseID']);
if(isset($_FILES['coverletter']['name'])) {
/*$imgid = explode ('.', $_FILES['coverletter']['name']);
$checktype = in_array($imgid[1], array('docx', 'doc', 'wps', 'word', 'pdf'));*/
// if($checktype == 1) {
$coverPath = DOC_ROOT.'/uploads/cover/'.'advertise_'.$_POST['iAdvertiseID'].'/'.$this->userID.'/';
if (!is_dir($coverPath)) {
if (!mkdir($coverPath, 0777, TRUE)) {
exit('dir not created.');
}
}
$config['upload_path'] = $coverPath;
$config['allowed_types'] = '*';
/*$config['max_size'] = '10000';
$config['max_width'] = '2000';
$config['max_height'] = '2000';*/
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('coverletter')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('application_form/application_form_view', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$img_file_path = $data['upload_data']['file_name'];
$this->db->update('tbl_application', array('vCoverLetter' => $img_file_path), array('iApplicationID' => $iApplicationID));
}
// } else {
// echo "Different File formate";
// }
}
if(isset($_FILES['resume']['name'])) {
/*$resid = explode ('.', $_FILES['resume']['name']);
$checktype = in_array($resid[1], array('docx', 'doc','wps','word','pdf'));*/
// if($checktype == 1) {
$resumePath = DOC_ROOT.'/uploads/resume/'.'advertise_'.$_POST['iAdvertiseID'].'/'.$this->userID.'/';
if (!is_dir($resumePath)) {
if (!mkdir($resumePath, 0777, TRUE)) {
exit('dir not created.');
}
}
$config['upload_path'] = $resumePath;
$config['allowed_types'] = '*';
/*$config['max_size'] = '10000';
$config['max_width'] = '2000';
$config['max_height'] = '2000';*/
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('resume')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('application_form/application_form_view', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$file_file_path = $data['upload_data']['file_name'];
$this->db->update('tbl_application', array('vResume' => $file_file_path), array('iApplicationID' => $iApplicationID));
// $this->thumbimg($data['upload_data']['full_path'], $data['upload_data']['file_name'], '0');
}
/*} else {
echo "Different File formate";
}*/
}