繁体   English   中英

Ext JS和PHP文件上传和解析错误

[英]Ext JS and PHP File upload and parsing error

我正在尝试上载具有多个记录的文本文件,但我不断收到语法()错误。 如果文件中只有一条记录,则已实现的逻辑可以正常工作。 问题可能是我在foreach循环中回显了多个记录吗?

描述:

用户浏览文件的Ext JS界面。 选择文件后,将其上传到服务器并用PHP进行解析。 解析后的进程称为processFile,由js文件调用。

我注意到的

  1. 如果文件中只有一条记录,则已实现的逻辑可以正常工作。
  2. json格式正确。
  3. 如果文件包含3条记录,则仍将读取第四个BLANK记录。

随附PHP代码:

<?php

$action = $_REQUEST['action'];
if ($action == 'uploadFile') {
  $fileName = $_FILES['file']['tmp_name'];
  $fileContent = file_get_contents($fileName);
  $fileInfo = parseFile($fileContent); //custom formatting 
    echo wrap_answer(array(
    'originalFile' => $fileContent,    
    'fileInfo' => $fileInfo,
    'success' => true
    ));  
}

if ($action == 'processFile') {
  $fileContent = $_REQUEST['file'];
  $fileInfo = parseFile($fileContent);  

  foreach($fileInfo['lines'] as $line) {        
    $input = array(
            'id' => $line['id'],
            'name' => $line['name']);              
    //custom function to insert records from file into clients table
    //function returns the inserted records ID  
    $insert_id = handler_Insert('clients',$input);    

    $success = ($insert_id == null || $insert_id == 0)?false:true;
    echo json_encode(array(
    'success' => $success)
    );    
    //NOTE:Processing records 1 by 1 and echoing the result
    //     Could this be the error? Any suggestions to better
    //     handle this process?
  }
}

任何帮助,建议等,不胜感激! 谢谢

解决了

我不得不重写代码以仅返回一个带有多个记录的json字符串,如下所示:

 //new variable to hold all array elements
    $rows = array();
    //In the foreach loop push the array item on $rows
    array_push($rows,array(
      'success' => $success,
      'record' => $input));    

    } //end of foreach loop
      echo json_encode(array(
      'id' => 'clients',
      'rows' => $rows));
   }//end of processFile function

然后在我的js文件中,我要做的就是使用Ext.each遍历结果,如下所示:

loadClient:function(result){
      var records = result.rows; //rows[0]{id,name},rows[1]      
      console.log(records);
      Ext.each(records,this.processKey,this);
    },
processKey:function(item,itemIndex){
      if(item.success){
         alert('Record inserted!');
      }
      else{
         alert('Failed to insert record');
      }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM