【问题标题】:Ajax File Upload in CodeigniterCodeigniter 中的 Ajax 文件上传
【发布时间】:2019-02-17 18:11:05
【问题描述】:

我的视图页面上有多个文件上传小部件,但作为参考,我在这里只添加了第一个,我想要做的是,我想使用按钮选择文件,然后通过 ajax 方法我想上传文件到目录并将文件名返回到名为“passport_copy_upload”字段的输入中。

喜欢这个

此页面上的所有其他文件上传小部件都可以使用,最后我们将提交表单并将用户带到感谢页面。

问题是:文件未上传到目录,在网络控制台中出现错误:{“错误”:“您没有选择要上传的文件。”}

我的观点

        <form id="fimilyVisaForm" action="https://www.mydomainurl.com/visa/add-other-details/<?php echo $appid;?>" method="post" enctype="multipart/form-data" onsubmit="return(validate());">

                                <h4 class="row marginBottom">Upload Documents</h4>

                <div class="form-control">
                    <div class="col-sm-4 label">Colored Scanned copy of the Passport</div>
                    <div class="col-sm-3">
                       <input type="text" class="form-control-input" placeholder="Colored Scanned copy of the Passport" name="passport_copy_upload" id="passport_copy_upload" onclick="removeError(this.id);" value="">
                    </div>   
                    <div class="col-sm-3">   
                       <input type="button" class="submit-btn read-more" value="Attach" id="passport">
                       <input type="file" name="passportimg" id="passportimg" style="display:none">
                       <a id="viewct" class="submit-btn read-more" href="#">View</a>
                      <div class="labelInfo">Colored Scanned copy of the Passport</div>
                    </div>
                </div>
    </form>

<script>
  document.getElementById('passport').onclick = function() {
    document.getElementById('passportimg').click();
};
</script>

Javascript Ajax 代码

<script>
$("#passportimg").change(function() {
    var file_data = $("#passportimg").prop("files")[0];
    var form_data = new FormData();
    form_data.append("file", file_data);
    $.ajax({
        url:'<?= base_url();?>index/do_upload',
        dataType: 'json',
        cache: false,
        async: false,
        contentType: false,
        processData: false,
        data: form_data,                         
        type: 'post',
        success:function(data)  
                     {  
                          console.log(data);
                     } 
    });
});
</script>

这是我的控制器

    public function do_upload() { 
      header('Content-Type: application/json');

      $config['upload_path']   = '<?= base_url();?>uploads/applicant/'; 
      $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
      $config['max_size']      = 2048;
      $this->load->library('upload', $config);
          $this->upload->initialize($config);

      if ( ! $this->upload->do_upload('passportimg')) {
         $error = array('error' => $this->upload->display_errors()); 
         echo json_encode($error);
      }else { 
         $data = $this->upload->data();
         $success = ['success'=>$data['file_name']];
         echo json_encode($success);
      } 
   } 

【问题讨论】:

  • 对,有什么问题?
  • @sorry utkanos - 问题是,文件没有上传到目录中
  • 你检查过网络控制台看看请求的结果是什么吗?您是否确认接收服务器端脚本接收到您认为的数据? (例如var_dump($_FILES)
  • @Utkanos : 我检查了网络控制台,那里的响应是空白的。
  • 你可以在stack overflow question找到你的解决方案

标签: php codeigniter-3 ajaxform asyncfileupload jsajaxfileuploader


【解决方案1】:

请替换:

var file_data = $("#passportimg").prop("files")[0];
var form_data = new FormData();
form_data.append("file", file_data);

收件人:

var form = $("#fimilyVisaForm")[0];
var form_data = new FormData(form);

【讨论】:

  • 我试过这个,我得到:{错误:“

    上传路径似乎无效。

    ”}错误:“

    上传路径似乎无效。

    "
  • 对不起上面的评论,我改变了目录,它上传了文件,现在我需要一点帮助,如何在输入框中将文件名恢复到我的视图中。非常感谢。
  • 来自您的模型:return $config['passportimg'][&lt;file path attribute here...&gt;],然后从您的控制器回显。那就好了。
猜你喜欢
  • 2015-12-26
  • 1970-01-01
  • 2012-05-25
  • 2012-07-18
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
相关资源
最近更新 更多