duandu1377 2018-02-16 08:03
浏览 96
已采纳

无法使用ajax上传文件

I am trying to upload form data using ajax but the issue is that I am not able to upload image/file.

Form Code

<form class="form-inline" id="form_add"  enctype="multipart/form-data"> 
  <input type="file" id="file-input" name="file-input"  accept="image/*" >

  <input type="text" class="form-control name" id="fname" placeholder="First Name" >                            
  <select class="location" id="location" >
    <option value="">Location</option>
    <?php foreach($location as $loc): ?>
      <option value="<?php echo $loc->city.', '.$loc->state;?>" ><?php echo $loc->city.', '.$loc->state;?></option>
    <?php endforeach; ?>
  </select>
  <button type="submit" class="save_btn"  id="submit" > <img src="save.png" class="Save">   </button>
</form>

Script Code

<script>
  $("#submit").click(function()
    {
      event.preventDefault();
      var filename = $('input[type=file]').val();
      var fname = $("#fname").val();
      var location = $("#location").val();
      if(filename != "" || fname != "" || location != "")
            {
              $.ajax({
                type: "POST",
                url: "Data/add_data",
                data: {
                        filename : filename,
                        fname:fname,
                        location:location
                      },
                cache: false,
                success: function(result){
                  console.log(result);
                }});
            }
    });
</script>

Backend Code

$ImageFile = $this->input->post('filename');
$config['upload_path'] = './assets/client_img/.';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);

if (!$this->upload->do_upload('ImageFile')) 
    {
     $error1 = array('error' => $this->upload->display_errors());
     print_r($error1);
    }
else
    {
      $data1 = $this->upload->data();
      echo $data1['file_name'];
    }

In the backend code I am getting the value of $ImageFile as C:\fakepath\pic.jpg but the file is not getting uploaded and the error says that

You did not select a file to upload

Can anyone please tell how i can upload the file

  • 写回答

4条回答 默认 最新

  • doumen1883 2018-02-16 08:30
    关注
    $("#submit").click(function ()
    {
        event.preventDefault();
        var filename = $('input[type=file]').val();
        var fname = $("#fname").val();
        var location = $("#location").val();
    
    
        if (filename != "" || fname != "" || location != "")
        {
            var formData = new FormData();
            formData.append('filename', $('input[type=file]')[0].files[0]);
            formData.append('fname', fname);
            formData.append('location', location);
            $.ajax({
                type: "POST",
                url: "Data/add_data",
                data: formData,
                contentType: false,
                processData: false,
                cache: false,
                success: function (result) {
                    console.log(result);
                }});
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!