0

我正在尝试通过 ajax _call 将文件表单视图发送到服务器。这是我的表格:

<form  id="fileForm" name="fileForm" enctype="multipart/form-data">
      <div class="row" >
        <div class="span8" >
            <label><strong>Please attach a file</strong></label>
            <input style="margin-bottom:0" type="file" name="attach" id="attach" />
            <button type="submit" class="btn btn-primary" onclick="do_upload()" ><i class="icon-camera icon-white"></i>Send</button> 
        </div>
    </div>
</form>

这是 do_upload (在同一个文件中):

<script type="text/javascript">
function do_upload() 
    {
      var uploaded_file= $('#attach').val();
      alert(uploaded_file); //prints the name of the  uploaded file
      $.ajax({
            type:'POST',
            url : '<?php echo site_url()."/public/hours/do_upload/"; ?>', //the path to my controller
            data : uploaded_file,
            cache: false,
            contentType: 'multipart/form-data',
            processData :false , 
            success:function(data){
                alert(' sucessful ajax call ');//is printed
                   }
                });     
            }

</script>

最后在公共/时间(这是我的控制器),我有:

function do_upload () {
    $config= array ('upload_path'=>'./uploads/','allowed_types'=>'pdf|gif|jpg|jpeg|docx',  'max_size'=>2048);

    //loading upload
    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('attach'))
          { $data = array('error' => $this->upload->display_errors());
            var_dump($data);
            exit;

           }
         else
         {  $data = array('upload_data' => $this->upload->data('attach'));
            var_dump($data);
            exit;
         }
       }

这是 var_dump($data) 的结果:

array(1) {
  ["error"]=>
  string(43) "<p>You did not select a file to upload.</p>"
}

萤火虫显示此错误:

Form contains a file input, but is missing method=POST and enctype=multipart/form-data on the form.  The file will not be sent.

有人可以帮我吗?谢谢

4

1 回答 1

1

尝试 type="button" 或使用<div class="btn btn-primary" onclick="do_upload()">..</div>on

<button type="submit" class="btn btn-primary" onclick="do_upload()" ><i class="icon-camera icon-white"></i>Send</button>

它对我有用..

于 2013-05-27T11:58:27.517 回答