簡體   English   中英

ajax jquery - 將 csv 文件發布到 ZE1BFD762321E409CEE364AC0B6E841 一行

[英]ajax jquery - post csv file to php one line at a time

我想做這個:

  • 用戶加載 csv 文件
  • 它一一讀取數據
  • php 進行處理,需要相當長的時間
  • php 進入流程到 mysql

問題是:

  • csv 文件太大,所以 php 達到了 30 秒的時間限制。
  • 處理后的數據要依次一一輸入到sql
  • running just the php, the connection to mysql can't keep up, so far i need to separate the csv to 30 lines per file so it could be entered correctly to mysql.

我認為結合 jquery:

  • jquery 讀取文件
  • jquery 每 csv 行暫停 1 秒
  • jquery 是客戶端所以沒有時間限制
  • jquery 然后調用 php 依次發送到 mysql

我堅持使用 jquery,因為它以無序方式發送 csv 中的數據。

我需要幫助的是暫停 jquery.post 在示例中每個 1 秒間隔。

這是我到目前為止提出的代碼。 我需要每個帖子的“暫停”效果

<script>
$(function () {
var o1;
var o2;
$.ajaxSetup({cache: false});

$('#load').click(function () {
    $.get($('#file').val(), function (content) {
        //get the file and convert into single array
        o1 = content.split(new RegExp("\n")).map(function (element) {return $.trim(element).toLowerCase();})

        //---------------------------------------------------------------------------- Display
        $("#box").empty();
        $.each(o1,
            function( i, j ){
                if (j!=""){
                    //add the tr
                    //$("#tbl").append($( "<tr id=\"tr-"+i+"\"></tr>" ));

                    //split again
                    o2 = j.split(new RegExp(",|\r")).map(function (element) {return $.trim(element).toLowerCase();});
                    $.post('test2.php',{jquery:'1' , d:o2[0] , o:o2[1] , h:o2[2] , l:o2[3] , c:o2[4]},function(res){
                        $("#box").append(res);
                    });
                }//end if
            }//end function
        );//end each
        //---------------------------------------------------------------------------- end of display

     });
  });
});
</script>
</head>
<body>
<form>
<input id="file" type="file" />
<input type="button" id="load" value="Load CSV" />
</form>
<hr />
<table id="tbl" border="1"><tbody></tbody></table>
<div id="box"></div>
<ol id="list"></ol>
</body>

這是“test2.php”文件內容。 這只是我將要做的一個例子,真正的代碼當然更復雜:

if ($_POST){
    @$_POST['jquery']!=''?$jquery=$_POST['jquery']:$jquery='';
    if ($jquery!=''){
        @$_POST['d']!=''?$d=$_POST['d']:$d='';
        @$_POST['o']!=''?$o=$_POST['o']:$o='';
        @$_POST['h']!=''?$h=$_POST['h']:$h='';
        @$_POST['l']!=''?$l=$_POST['l']:$l='';
        @$_POST['c']!=''?$c=$_POST['c']:$c='';
        echo 'D: '.$d.' - '.$o.' - '.$h.' - '.$l.' - '.$c."<br />\n";
    }
}

這里真的需要幫助。 謝謝。

即使我不是您解決問題的方式的堅定支持者...

停止 jQuery 進行異步調用async: false

您可以在 jQuery 中執行此操作: $.ajaxSetup({async:false}); 然后定期撥打$.post電話。 有關手冊的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM