2014-12-04 30 views
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; 
} 

有人能帮助我在此先感谢?

+0

什么的print_r($ _ FILES)的'输出;'? – 2014-12-04 03:52:52

+0

输出为空:( – 2014-12-04 04:50:27

+1

啊,刚刚看到你试图用AJAX上传,这实际上是不可能的,但有这个解决方法:http://www.ajaxf1.com/tutorial/ajax-file-upload- tutorial.html – 2014-12-04 05:01:27

回答

1

原因是使用HTML上传文件并不像您想象的那么简单。这里有两个很好的例子普通帖子看起来像(在HTTP协议)与一个多/表单数据请求的样子:

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

事情采取从这里走,也表单提交技术上来说,使用文件上传形式提交是两件非常不同的事情。

$ .post只能为您提交正常表单提交,jQuery不支持文件上传。

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

干杯, 马蒂亚斯