PHPExcel尝试使用ajax()调用读取Excel文件失败

时间:2017-08-24 04:20:59

标签: ajax phpexcel

我试图通过ajax调用使用PHPExcel Lib读取Excel文件。但是,读取尝试导致错误<ImageView android:layout_width="0dp" android:layout_height="0dp" android:adjustViewBounds="true" android:scaleType="centerCrop" android:src="@drawable/signup_background" card_view:layout_constraintBottom_toBottomOf="parent" card_view:layout_constraintLeft_toLeftOf="parent" card_view:layout_constraintRight_toRightOf="parent" card_view:layout_constraintTop_toTopOf="parent" />

我的index.php如下: -

PHPExcel_Reader_Exception: Could not open for reading! File does not exist.

ajax调用php文件readxlxs.php如下: -

<?php
    ob_start();
    ini_set("xdebug.var_display_max_children", -1);
    ini_set("xdebug.var_display_max_data", -1);
    ini_set("xdebug.var_display_max_depth", -1);    
    session_start();

    if ( isset($_POST['label']) ) {
    var_dump($_POST['label']);
    }

?>  


<!doctype html>
<html lang="en">
<head>
    <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
    <script>
    var label;
    function checkfile(sender) {    
      var validExts = new Array(".xlsx", ".xls");   
      var fileExt = sender; 
      fileExt = fileExt.substring(fileExt.lastIndexOf('.'));    
      if (validExts.indexOf(fileExt) < 0) { 
        alert("Invalid file selected, valid files are of " +    
          validExts.toString() + " types.");    
        return false;   
      } else return true;   
    }   

    $(function() {  
          $(document).on('change', ':file', function() {    
            var input = $(this),    
                numFiles = input.get(0).files ? input.get(0).files.length : 1,  
                label = input.val().replace(/\\/g, '/').replace(/.*\//, '');    

            input.trigger('fileselect', [numFiles, label]); 

        }); 
        $(document).ready( function() {     
          $(':file').on('fileselect', function(event, numFiles, label) {        

                if(checkfile(label)== true){         
                    var input = $(this).parents('.input-group').find(':text'),          
                      log = label;              
                    if(input.length) {            
                      input.val(log);               
                    } else {              
                     // if(log) alert(log);             
                    }
                }       
                 $.ajax({  
                    //url:"../uploadxlsx/readxlsx.php",     
                    url:"uploadxlsx/readxlsx.php",                          
                    method:"POST",              
                    data:{label:label },            
                 success:function(data){            
                         $('#sheetnames').html(data);           
                    }  
            });
          });   

        });     
    }); 
</script>
</head>

<body>
 <br>
 <br>
 <br>
    <div class="container">
        <table>                 
            <thead>                 
                <div class="input-group">                                   
                            <label class="input-group-btn">     
                                <span class="btn btn-primary">      
                                    Browse <input type="file" id="uploadfile" name="uploadfile" style="display: none;" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel">        
                                </span>     
                            </label>        
                            <input type="text" class="form-control" readonly="">        
                </div>                          
            </thead>                
        </table>                    
    </div>
    <div class="input-group" name='sheetnames' id='sheetnames'>

                            </div>
  </body>
</html>

显示错误的屏幕截图是

enter image description here

错误在哪里?或者是PHPExcel不能与ajax一起使用?

1 个答案:

答案 0 :(得分:0)

一般建议

  • 您可以使用浏览器调试控制台(如果是chrome,则为F12)以查看文件是否实际上传
  • 你可以用var_dump($ _ FILES)来查看实际上有什么服务器
  • 这与开始ajax vs PHPExcel无关,我认为你在如何处理PHP中的表单数据的基本层面上弄错了

以及您的问题,因为您的文件输入是这样的

<input type="file" id="uploadfile" name="uploadfile"

变量名必须是

$_FILES["uploadfile"]

和“name”只是名称,对于您必须使用的实际文件

$_FILES["uploadfile"]["tmp_name"]

请阅读more info on $_FILES array structure

Array
(
    [file1] => Array
        (
            [name] => MyFile.txt (comes from the browser, so treat as tainted)
            [type] => text/plain  (not sure where it gets this from - assume the browser, so treat as tainted)
            [tmp_name] => /tmp/php/php1h4j1o (could be anywhere on your system, depending on your config settings, but the user has no control, so this isn't tainted)
            [error] => UPLOAD_ERR_OK  (= 0)
            [size] => 123   (the size in bytes)
        )
)