【问题标题】:CodeIgniter, Jquery Ajax, Form Submission and File UploadCodeIgniter、Jquery Ajax、表单提交和文件上传
【发布时间】:2014-03-26 02:55:19
【问题描述】:

我正在使用 CodeIgnitor 框架开发一个网站。

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

我的 HTML 是:仅给出以下 HTML 的相关部分

  <form method="post" action="" id="upload_file" enctype="multipart/form-data">
  <div id="inner2" style="display:none">
    <div class="trainingcontent" style="height:400px;">
      <div class="chaptername" >
        <p>Chapter Name</p>
        <input type="text" id="cname" name="cname" class="awesome-text-box" />
        <input type="hidden" id="training_id" name="training_id" />
      </div>
      <div class="trainingurl">
        <p>Training Video</p>
        <input type="text" id="video_url" name="video_url" class="awesome-text-box" placeholder="Paste your video URL here..."/>
      </div>
      <div class="uploadpdf">
        <p>Upload PDF</p>
        <a href="#"><div class="uploadbutton">
          <div class="uploadtext"> <input type="file" name="userfile" id="userfile" size="20" />UPLOAD PDF </div>
        </div></a>
      </div>
</form>

我的 JQUERY 代码是

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

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

我尝试了各种方法,但没有任何效果。

【问题讨论】:

标签: jquery ajax forms codeigniter file-upload


【解决方案1】:

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

参考链接: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!");
    });


}

【讨论】:

  • 我尝试了上述解决方案,但出现错误 SyntaxError: JSON.parse: unexpected character var data = JSON.parse(XMLHttpRequest.responseText);
  • 文件上传了吗?删除 dataType=json
  • 错误是因为我使用来自服务器端代码的 json 响应。所以只需从完整的回调中删除该行并删除 dataType='json'。我更新了答案。
  • 很晚了,但这对我也有帮助。非常感谢拉维!
猜你喜欢
  • 2012-07-05
  • 2017-03-03
  • 2013-03-04
  • 2013-07-08
  • 1970-01-01
  • 2013-01-08
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
相关资源
最近更新 更多