CodeIgniter,Jquery Ajax,表单提交和文件上传

我正在使用CodeIgnitor Framework在一个网站上工作。

问题与CodeIgniter,Jquery Ajax,表单提交和文件上载有关

我的HTML是:仅提供以下HTML的相关部分

Chapter Name

Training Video

Upload PDF

UPLOAD PDF

我的JQUERY CODE是

 function abc(){ var training_id = $('#training_id').attr('value'); var cname = $('#cname').attr('value'); var video_url = $('#video_url').attr('value'); $.ajax({ type: "POST", url: "../chapters/create", data: "training_id="+training_id+"&cname="+cname+"&video_url="+video_url, success: function(returned_html){ echo returned_html; } }); }//end of function abc 

如何将输入类型文件数据传递给控制器​​?

我尝试了各种方法但没有任何作用。

我在codeigniter中使用JS的formData对象创建了类似的function

参考链接: https : //developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects#Sending_files_using_a_FormData_object

示例代码:

 function abc(){ // create a FormData Object using your form dom element var form = new FormData(document.getElementById('upload_file')); //append files var file = document.getElementById('userfile').files[0]; if (file) { form.append('userfile', file); } //call ajax $.ajax({ url: "../chapters/create", type: 'POST', data: form, cache: false, contentType: false, //must, tell jQuery not to process the data processData: false, //must, tell jQuery not to set contentType success: function(data) { console.log(data); }, complete: function(XMLHttpRequest) { var data = XMLHttpRequest.responseText; console.log(data); }, error: function() { alert("ERROR"); } }).done(function() { console.log('Done'); }).fail(function() { alert("fail!"); }); }