【问题标题】:Ext JS and PHP File upload and parsing errorExt JS 和 PHP 文件上传和解析错误
【发布时间】:2010-10-11 10:55:09
【问题描述】:

我正在尝试上传包含多条记录的文本文件,但我不断收到 syntax() 错误。 如果文件中只有一条记录,则实现的逻辑可以正常工作。 问题可能是我在 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?
  }
}

非常感谢任何帮助、建议等! 谢谢

【问题讨论】:

    标签: php javascript ajax extjs weblogic


    【解决方案1】:

    已解决

    我不得不重写我的代码以只返回一个包含多条记录的 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 文件中,我所要做的就是使用循环遍历结果 像这样的分机:

    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');
          }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-25
      • 2016-07-25
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 2021-09-06
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多