0

我正在制作一个使用 excel_reader2.php 将 excel 导入数据库的应用程序。我创建了一个用于上传excel文件的表单,以及当我要上传的文件时,我想读取Excel文件的数据结合表。当我使用 js 时,这成为问题,我无法在 php 代码中解析 $ _FILES。

<script type="text/javascript">
function sheetChange()
{
    var html;
    $("#sheetName td").remove();
    var fileInputContent = $('#form').serializeArray();
    $.post(basedomain+"mycontroller/readSheet",fileInputContent,function(result)
    {
       if(result)
       {
          $("#sheetName").show();
          var data = JSON.parse(result);
          html += '<td>Sheet Name</td><td colspan=\'3\'><select name=\'SHEET\' required>';
          for(var i in data)
          {
             html += '<option value=\''+data[i]+'\'>'+data[i]+'</option>';
          }
          html +='</select></td>';
          $("#sheetName").append(html);
       }
     });
  }
</script>
<form id = 'form' method="post" action="upload.php" enctype="multipart/form-data">
    <table cellspacing="0" cellpadding="0">
       <tr>
           <td>Input Xls File</td>
           <td colspan="3"><input type="file" name="file" id="file" onchange="sheetChange()"/></td>
       </tr>
      <tr id="sheetName"></tr>
    </table>
</form>

php代码:

public function readSheet()
{
    error_reporting(E_ALL ^ E_NOTICE);
    require_once 'excel_reader2.php';
    $data = new Spreadsheet_Excel_Reader($_FILES['file']['tmp_name']); //$_FILES is null

    foreach ($data->boundsheets as $k=>$sheet)
    {
        $row[] = $sheet['name'];
    }
    echo json_encode($row);
    exit;
}

任何人都可以帮助我吗?在此先感谢。

4

1 回答 1

1

原因是使用 HTML 上传文件并不像您想象的那么简单。这里有两个很好的例子,一个普通的 POST(在 HTTP 协议中)的样子与一个 multipart/form-data 请求的样子:

http://www.htmlcodetutorial.com/forms/form_enctype.html

从这里要带走的是,表单提交和带有文件上传的表单提交在技术上是两个非常不同的东西。

$.post 只能为你做普通的表单提交,jQuery 不支持文件上传。

有两种方法可以解决这个问题:

干杯,马蒂亚斯

于 2014-12-04T07:01:48.100 回答